Uncaught SyntaxError: Unexpected token : - javascript

I used simple jquery method to access data from other url with JSON. But when submit i get a error on the json file. What is that suppose to mean? I get this error.
Uncaught SyntaxError: Unexpected token :
$.getJSON('http://curvefever.com/achtung/match/16911009/json?callback=?', null, function(data) {
alert("it worked");
});

The use of callback=? is telling jQuery to make a request for JSONP.
The server is responding with JSON (but claiming it is HTML).
JSON is not JSONP so trying to "parse" it as such throws an error.
("parse" is in scare quotes because JSONP isn't so much parsed as executed).
Get rid of the callback=? to try to fetch the data using XMLHttpRequest instead of <script>. Note, however, that unless you are running the script on http://curvefever.com, you will get a cross-origin error since the server is not responding with CORS headers.
Unless you can change the server to return JSONP or to respond with CORS headers, you will need to use a proxy to access the data from client side JavaScript.

Related

something wrong with jsonp data, How to get the data

But it returns -403 why
but when I click that, It comes out this
how can I get the message
The HTTP 403 Forbidden client error status response code indicates that the server understood the request but refuses to authorize it.. After one-line check with Fetch API the answer is: server is not handling Cross Origin Resource Sharing - making cross-origin AJAX is not possible. You can avoid it by using simple local HTTP server (e.g. in Python) to get data from server (e.g. with requests) and self-serve it (e.g. with Flask).
Check it in JS console:
fetch('https://api.bilibili.com/x/web-show/res/loc?jsonp=jsonp&pf=7&id=1695').then(r=>r.json()).then(json=>console.log(json))

Cross Domain Ajax (JSONP) Callback Issue

I have a code where i need to make cross domain requests to get the text/html as response. I've been using the JSONP method and was getting the results (json) before on some other program. When i run the program, console reports an error which is
Uncaught SyntaxError: Unexpected token <
?callback=jQuery17207555819984991103_1414157672145&_=1414157675836"
As you can see it has added an extra parameter value after the callback parameter which is causing an issue. When i see the response in Network tab, its pretty good and exactly what i wanted. I tested it on 4 cross domain links and that extra paramter is coming to all of them.
Following is my code:
$.ajax({
type: 'get',
url: link,
dataType: 'jsonp',
success: function(dataWeGotViaJsonp){
alert(dataWeGotViaJsonp)
var len = dataWeGotViaJsonp.length;
}
});
The links I have passed to it:
http://stackoverflow.com/questions/20944962/data-grid-view-update-edit-and-delete-in-vb-net-windows-form-that-using-multipl
http://www.espncricinfo.com/pakistan-v-australia-2014/engine/match/727927.html
http://pucit.edu.pk/
Response is good but due to that error, success callback isn't being called. Any solutions?
"Uncaught SyntaxError: Unexpected token <" is an indication that the returned data is very likely HTML mark-up and not proper JSONP. In order to return HTML from a JSONP web service, you need something on the server that is wrapping the HTML in proper procedure call syntax. E.g.,
jQuery17207555819984991103_1414157672145 ("<div>... your HTML here ...</div>")
Since the HTML will likely have quote characters in it somewhere, you will need to escape, URL encode, UUEncode, or otherwise convert the HTML text to be an appropriate Javascript string, and then convert it back in the client.
As for the "_=1414157675836", this is being added to ensure the response is not cached. Unless your server's web service is rejecting the request because the "_" parameter is not recognized, this is a red herring. The problem is the bad JSONP syntax coming from the host.

JavaScript REST Request without AJAX (omit JSONP)?

I'm using JSONP to send a REST Request, and I can see the response (json) in the chrome dev tools but can't use the result because I'm getting the error "unexpected token :". As you can read in linked question, this is because a function is expected as response (i get json).
Because I can't modify the webservice I need to access the webservice without AJAX so I dont have those problems with "same origin policy" and can use normal json as the data type. Every Example I can find is using AJAX. Isn't there another possibility?

jQuery get JSON

I'm trying to pull in the total shares from a number of pages and have them displayed on my home page. I'm using addthis for the share buttons. I found a way to pull in the required info using JSON here.
Since I have multiple urls I'm storing them in an array, then cycling through and calling each one. Here's my code...
jQuery(document).ready(function(){
var shareCountArray = [ "url1", //Array to put all url's to get share total
"url2"]
shareCountArray.forEach( function(shareUrl) {
var url = "//api-public.addthis.com/url/shares.json?url=http%3A%2F%2F"+ shareUrl +"?callback=?";
jQuery.getJSON(url, function(json) {
alert(json.shares);
});
});
});
It's throwing up the error "Uncaught SyntaxError: Unexpected token : ". I thought this may have been because I included ?callback=? but when I remove that the console throws up errors because they're different origins.
Thanks for your time!
When you include callback=? then jQuery thinks the response is JSONP. JSONP is nothing else than including a JavaScript file. I.e. the response you receive is interpreted as JavaScript. That also means that if the server returns anything else than valid JavaScript, you will get an error. JSON on its own is not valid JS syntax, that's why you get the that error (you can verify that easily by putting {"foo": 42} in the console).
when I remove that the console throws up errors because they're different origins.
JSONP as well as CORS have to be supported by the server. You can't make an Ajax or JSONP request to the server if it doesn't. See Ways to circumvent the same-origin policy for alternatives.
But it actually looks like the service does support JSONP:
When calling services that support html responses, you may omit the .html extension. Optionally, you can use json mode and pass callback=function to specify a jsonp callback wrapper. Content-Type headers are set on responses as appropriate.
Looking at your URL, it is malformed. You should use &callback=? instead of ?callback=?. Multiple request parameters in a URL are separated by &. ? only indicates the beginning of the query string. E.g.
http://example.com/?param1=value1&param2=value2
Learn more about URLs: https://en.wikipedia.org/wiki/Url

Chrome cannot parse single line JSONP object

I'm having issues getting this JSON object to load via JSONP in Chrome 18 (latest stable). For some reason it presents me the error: Uncaught SyntaxError: Unexpected token :. According to JSONLint, the response is a valid JSON object.
Here is the code example: http://jsfiddle.net/jakebellacera/2j7DL/1/
To add: I'm attempting to get around cross-domain, I have permissions to both servers, but we cannot allow cross-domain on a specific domain for specific reasons. If there is a better way to do this, please let me know.
The server at lpunderground.com is returning JSON, not JSONP.
What's special about JSONP is that it's a valid javascript statement - the data returned from the server is wrapped in a function call (or sometimes a variable assignment) and when returned it is evaluated as normal javascript. Typically the client passes the server in the request parameters a function name to use in wrapping the data.
See the Wikipedia article and the jQuery docs for the details.

Categories

Resources