ReactJS - file requested from server twice - reactjs

I'm starting out with ReactJS and I'm following the simple example in the "Gettind Started".
This is my HTML file:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React!</title>
<script src="js/reactjs/react.min.js"></script>
<script src="js/reactjs/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<script type="text/babel" src="js/main.js"></script>
</head>
<body>
<div id="example"></div>
</body>
</html>
It works. But if I watch the network traffic with Fiddler, I see that main.js is pulled twice from the server. Is that on purpose? bug?

As soon as browser encounters following lines
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<script type="text/babel" src="js/main.js"></script>
browser issues http requests for both files. But since js/main.js is of 'text/babel' type for browser it won't be given to JavaScript engine to parse/execute. Rightly so, because the 'main.js' might have ES6 code which browser won't understand as it now. Once babel's browser.min.js loads and executes, it searches for script tag in DOM with type 'text/babel' and then issue XHR request to load that file. After that browser.min.js compiles the code in 'main.js' transform it into ES5 and then executes it. This is the way it works.
Since browser already has 'js/main.js' in its cache from earlier request, the XHR request issued by babel's browser.min.js for 'js/main.js' is served from the cache itself, so there won't be any additional external http request.

Related

Chrome Failted to Load Resources Error 404 How to Fix for lightbox2

Failed to load resource: the server responded with a status of 404 (Not Found) mistakes shows himself in console for Lighbtox.js. I downloaded the file and put into the my js file, I put the url path corrcetly to the bottom of my project after bootstrap and jquery js files.
I downloaded the file from original place.
I created a minimal working example as follows. It does work with the latest jquery. Make sure jquery script tag is before the lightbox script tag.
my index.html
<html>
<head>
<script src="js/jquery-3.4.1.min.js"></script>
<link href="css/lightbox.min.css" rel="stylesheet" />
</head>
<body>
Image #1
<script src="js/lightbox.min.js"></script>
</body>
</html>
File structure
And the result

AngularJS app written two years ago, trying to get it working, lots of errors in the console

enter image description hereI wrote an app in AngularJS two years ago. It was my first stab at AngularJS in a coding bootcamp. I haven't coded much in 2-years. I never hosted the app. It was working as intended the last time I used it. I opened it up today in Chrome and it no longer works. I'm getting a bunch of errors in the console. Is it possible to fix it and get it running again or do I need to start over with the newest version of AngularJS?
Your problem was:
1.your links to libraries were in body and not in header.
2.all your links were missing https:
3.You might running from file and not in local server.
Problem Solved:
I run it in localhost Apache server.
I changed all your links with https: included
I removed all links inside header
Below is your new index.html file.
Copy and paste this new code and run in local server.
<!DOCTYPE html>
<html ng-app="oppApp">
<head>
<title>workMe</title>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/angular_material/0.8.2/angular-material.min.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="style.css">
<!-- Angular-->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.min.js"></script>
<script src="https://code.angularjs.org/1.4.0-beta.5/angular-route.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular-animate.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular-aria.min.js"></script>
<!--Material-->
<script src="https://ajax.googleapis.com/ajax/libs/angular_material/0.8.2/angular-material.min.js"></script>
<!-- Firebase -->
<script src="https://cdn.firebase.com/js/client/2.2.2/firebase.js"></script>
<!-- AngularFire -->
<script src="https://cdn.firebase.com/libs/angularfire/1.0.0/angularfire.min.js"></script>
<script src="js/app.js"></script>
<script src="js/NewOpp/newOppCtrl.js"></script>
<script src="js/OppService.js"></script>
</head>
<body>
<header class="header">workMe</header>
<div ng-view class="background"></div>
</body>
</html>
Look at the result. and console with no errors

Add AngularJS frontend to Spring Boot application

I've generated a Spring Boot application by using Spring Initializr. This is the screenshot of my resources directory.
I added Angular dependencies by using <script> tag in index.html
<!DOCTYPE html>
<html lang="en" ng-app="companyApp">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-route.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-resource.min.js"></script>
<script src="app.js"></script>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Hello, Spring!
</body>
</html>
My main application class is configured with #SpringBootApplication annotation.
Once I open the page, I get this error in the console
Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.0/$injector/modulerr?p0=companyApp&p1=Error…ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.5.0%2Fangular.min.js%3A20%3A449)
From documentation I see that some module failed to load.
Should I include any other libraries in the index.html page to fix this error?
Solved the issue. Just use Bower to download needed dependencies and add them to /static directory.
Include in index.html
<!DOCTYPE html>
<html lang="en">
<script src="bower_components/angular/angular.min.js"></script>
<script src="bower_components/angular-resource/angular-resource.min.js"></script>
<script src="bower_components/angular-route/angular-route.min.js"></script>
<script src="app/app.js"></script>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body ng-app="companyApp">
Hello, Spring!
</body>
</html>
if you want to split your static contents in many ways:
put them separately on apache/nginx server then it will access your spring boot application throw REST, but with different IP "if different machine" or Port "on same machine".
spring boot application can get static content outside its fat jar if you put the in /static folder in same path of your fat jar file, and you can change this path in application.properties using spring.resources.staticLocations property check here
finally you can make jar project to contains your static contents and add this project using maven as dependency to your spring boot project, do not forget your static contents have to be in /resources/static
which option to use based on you case and you project size but it is recommended to split it if you have different developers working on front/back end.
Just add AngularJS using Spring Initializr and it will do it all for you, or do it separately and copy how it generates the front-end layout. I personally have resources -> static -> app then have all of my partials within 'app', index.html under 'static' and application.properties under 'resources'.

Can't run project locally due to "Failed to load resource: net::ERR_EMPTY_RESPONSE" in Learning AngularJS Chapter 2 by Ken Williamson

I can't figure out why I'm getting the ERR_EMPTY_RESPONSES error below. I am working on Chapter 2 of Learning AngularJS by Ken Williamson. I typed in all the code exactly as it is in the book and set up the directory structure exactly as it is listed in the book. However, when I try to run the project (locally on my computer), I get the following error:
Failed to load resource: net::ERR_EMPTY_RESPONSE (22:27:47:397 | error, network)
at http://localhost:8383/AngularJsHelloWorld_chapter2/partials/main.html
Error: [$compile:tpload] http://errors.angularjs.org/1.5.0-rc.0/$compile/tpload?p0=partials%2Fmain.html&p1=-1&p2=
at Error (native)
The code for the relevant pages look like this:
Index.html:
<!DOCTYPE html>
<html lang="en" ng-app="helloWorldApp">
<head>
<title>AngularJS Hello World</title>
<meta name="viewport" content="width=device-width, initial- scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="js/libs/jquery-1.11.3.min.js"></script>
<script src="js/libs/angular.min.js"></script>
<script src="js/libs/angular-route.min.js"></script>
<script src="js/libs/angular-resource.min.js"></script>
<script src="js/libs/angular-cookies.min.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/services.js"></script>
</head>
<body>
<div ng-view></div>
</body>
</html>
main.html:
<div>{{message}}</div>
I realize I might need to provide additional information. Any help is appreciated.
Here is a link to the directory structure:
https://library.oreilly.com/book/0636920035831/learning-angularjs/14.xhtml?ref=toc#idp2844416
Here is the code on Github: https://library.oreilly.com/book/0636920035831/learning-angularjs/14.xhtml?ref=toc#idp2844416
By the way, my directory is slightly different from the book in that I have the latest versions of the js framework in it.
This error is caused by a known issue in ngRoute (https://docs.angularjs.org/api/ngRoute):
If ngView is contained in an asynchronously loaded template (e.g. in another directive's templateUrl or in a template loaded using ngInclude), then you need to make sure that $route is instantiated in time to capture the initial $locationChangeStart event and load the appropriate view. One way to achieve this is to have it as a dependency in a .run block: myModule.run(['$route', function() {}]);
Adding the following code should fix the error. It did for me when opening the web page locally.
helloWorldApp.run(['$route', function() {}]);

Issue reading from my Slim PHP API through Angularjs using ngResource

I've recently completed a Slip PHP API build which interacts beautifully with my Android application. I'm now building a web front-end and for the life of me I can't understand how to request information via GET using ngResource's query function.
My index.html file is:
<!doctype html>
<html ng-app="discussApp">
<head>
</head>
<body>
<div ng-controller="topicsController">
<div>{{ topics }}</div>
</div>
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="js/angular-resource.min.js"></script>
<script type="text/javascript" src="js/angular-route.min.js"></script>
<script type="text/javascript" src="js/app.js"></script>
</body>
</html>
My app.js file:
var app = angular.module('discussApp', ['ngResource']);
app.controller("topicsController", function($scope, $resource, $http) {
var Topics = $resource("http://api.discussorama.com/v1/topics");
$scope.topics = Topics.query();
});
And the information I'm trying to read (I've successfully done this both from Chrome's Advanced REST Client, the browser, and my Android app) is at http://api.discussorama.com/v1/topics (Note: I've temporarily disabled my authentication middleware in Slim PHP to test with Angularjs). The endpoint returns a json response but I'm not seeing anything in Angular besides "[]". The link to the Angular app is http://hwaelapps.com/discuss/web if anyone would like to see exactly what is happening. Thanks in advance for the help. Note: I've tried several ngResource config options including urlencode but it's still not receiving the data so I stripped all that and all the code is directly as is in the question.
The answer was in the console log all along. The error I was receiving was:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
The solution was simply to add
header("Access-Control-Allow-Origin: *");
to my Slim API's index.php file. I guess my shared host isn't configured for CORS.

Resources