VM892:1 Uncaught SyntaxError: Unexpected token e in JSON at position 0 - json

I'm presently working on a phx / phoenix API written in Elixir. And I have created a frontend for the API using React.js. However, I'm getting the below error message in the JS console of the browser.
I have successfully created a user using Postman, so I'm 99% sure the error isn't with the phx project, but rather somewhere with the React project.
I have both the frontend and backend hosted on github. And a .env file will need to be created in the root of the React project with the below line,
REACT_APP_API_URL=http://localhost:4000/api
and was working my way through the following tutorial.
Any and all help would greatly be appreciated.

The output of localStorage.getItem("token") being
eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJVc2VyOjEiLC‌​JleHAiOjE0ODcyODI4OD‌​csImlhdCI6MTQ4NDY5MD‌​g4NywiaXNzIjoiUGhvZW‌​5peENoYXQiLCJqdGkiOi‌​IwNzFlYzgwYi0wZmYzLT‌​QyYzgtODA3Mi1kNzViZm‌​VhZTg4NWEiLCJwZW0iOn‌​t9LCJzdWIiOiJVc2VyOj‌​EiLCJ0eXAiOiJhY2Nlc3‌​MifQ.NsuqH50HooK8vjF‌​fHtPH9iXSykZ9oYA0ul4‌​b_C5fQtpu_zFvNNy-skc‌​v9HI2i25X-NlB-9xOr-x‌​zh2abnrpYUw
suggests that for some reason, the app stored the token without passing it through JSON.stringify, and calling JSON.parse on this string throws the Unexpected token e error, as expected.
I did not see any localStorage.setItem without JSON.stringify in the current code, so the token was probably stored like that in a previous version of the app. You should try clearing it manually and logging in again.

When we get the non-JSON response, we get such error..
To avoid such error, mention the responseType: Text in your api endpoint call.
This will work,
return this.http.post(`${environment.apiUrl}/login`, user, {responseType: 'text'});
This will not work(If you mention type),
return this.http.post<string>(`${environment.apiUrl}/login`, user, {responseType: 'text'});

This error message usually means you're getting a non-JSON response. If you look at the raw response in the Network tab of your debugger, you should be able to see what you're getting back from the server.

Related

"Script Error Displaying unprocessed Data

I am currently following the mulesoft fundamentals 4 module 5. as i have been following the guide i should be able to Make requests to the API proxy from Exchange. However, every-time i click send i get an Error; Script error. Displaying unprocessed data"
The Error message Below:
Script error. Displaying unprocessed data.
''''''
The image displays the error code
Thank You for your help
This is because the server is unreachable or bad gateway (502).Make sure the implementation URL which you provide to create proxy is working.In my case I have used https instead of http.

SyntaxError: Unexpected token < in JSON at position 0

SyntaxError: Unexpected token < in JSON at position 0
while retrieving json data using ionic 2
here is the link http://areeef.unaux.com/php/platforms.php
here is the code i am using to get json data
constructor( public navCtrl: NavController, private http: Http) {
this.http.get('http://areeef.unaux.com/php/platforms.php').map(res => res.json()).subscribe(data => {
console.log(data);
}); }
when i run the same php code in local host then i can run the app without any error. but in online sever it is giving the error mentioned above
so is that a problem of the server or error in my code?
Nothing seems wrong with the code. The problem is because your server not returning valid JSON.
Check the API's behaviour using postman plugin. I have checked it and it returning invalid JSON-
This site requires Javascript to work, please enable Javascript in
your browser or use a browser with Javascript support
So, get your api working on postman first.
This error is normally caused by the server returning non JSON data. In this case, the PHP script could be returning an error message in form of HTML which will cause an error. The best approach for such errors is to debug the PHP script independently or accommodate the possibility of getting both html and JSON data on the client side.

Use oAuth token with Azure MobileServiceClient.login()

I am using the native Facebook SDK (through an opensource tool called 'SimpleFacebook') to authenticate with Facebook. That part is working great. I find the Microsoft Azure implementation of Facebook authentication to be lacking.
Anyway, the next step is to use the token from this Facebook session and authenticate with MS/Azure. There are two methods like look like they should do the job
public void login(java.lang.String provider,
java.lang.String oAuthToken,
UserAuthenticationCallback callback)
Invokes Windows Azure Mobile Service authentication using a provider-specific oAuth token
Parameters:
provider - The provider used for the authentication process
oAuthToken - The oAuth token used for authentication
callback - Callback to invoke when the authentication process finishes
And another very similar method where the second param is a JSON object of type:
com.google.gson.JsonObject oAuthToken,
Is it just me or is the documentation lacking here? I tried just calling the Facebook session's .getAccessToken() and passing that to the functions and I get an error from Azure:
Caused by: com.microsoft.windowsazure.mobileservices.MobileServiceException: {"code":400,"error":"Error: invalid json"}
at com.microsoft.windowsazure.mobileservices.MobileServiceConnection$1.onNext(MobileServiceConnection.java:115)
How do we know what the correct JSON format is?
Am Using the right token?
More information can be found at:
at this Azure site
I think I have this figured out. Essentially all I had to do was create a JSON object (which is fairly new for me). I tried this earlier but I had imported the wrong JSON class (I had imported org.json.JsonObject or something rather than the com.google.gson.JsonObject).
once I did that I had to figure out what the correct json properties should be. Through a lot of Google searches I found out this is the correct format:
JsonObject jo = new JsonObject();
jo.addProperty("access_token", token);
Then use jo.toString() in the call like:
mClient.login(MobileServiceAuthenticationProvider.Facebook, jo.toString(), new UserAuthenticationCallback() {
.....
}
Really not that difficult, but why wouldn't Azure team put this in their docs???
Maybe this is just "obvious" information for a seasoned dev, but it took me a whole evening to figure out.

cannot get document id using box view api

In my app, after get "https://dl.boxcloud.com/*" url and send it to the View API, I receive this error:
{
message: "JSON parse error - No JSON object could be decoded"
type: "error"
request_id: "3ef12abcaf7a4c5abab5fb0d3959255e"
}
you can use this chrome extension to recreate this error
https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo
I tried with the other rest clients and it work correctly. Except in my app and the extension above.
I tried with the other rest clients and it work correctly. Except in my app and the extension above.
If this is the case, it's likely that the client you're using isn't actually sending properly formatted JSON. One way to debug this would be to output the raw HTTP request the client is sending and ensuring that it's actually sending the JSON properly.

Not able to fetch the json response through angularjs

Need to fetch the build values from apache.org. So i am using their api
https://builds.apache.org/api/json
I tried angularjs $http.jsonp but not able to fetch the data.
In chrome console under network json api is getting loaded but the data is not getting returned instead it is throwing the response as error.
app.controller("jsoncontroller",function($scope,$http){
var url='https://builds.apache.org/api/json';
$http.jsonp(url).success(function(data){
console.log('success');
})
.error(function () {
console.log('error')
});
});
Getting the error as
Uncaught SyntaxError: Unexpected token :
error
As per the jsonp angular docs, you must append JSON_CALLBACK to the URL: https://builds.apache.org/api/json?jsonp=JSON_CALLBACK
However, that URL doesn't work because even when the callback parameter is specified, the server still sends back a content-type of application/json, instead of the expected application/javascript. This causes it to be parsed (evidently) by the json parser instead of the javascript callback needed for JSONP to work. I'm not versed enough in JSONP or Angular to know who is it fault here.
I've made a fiddle with this working with another URL.
[Update]: The apache build server appears to use Jenkins, which has disable JSONP from the remote API. You can verify this yourself by trying to hit their jsonp endpoint, which returns a 403. You'll have to use another endpoint, no way I can see around this.