Chrome cannot parse single line JSONP object - javascript

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.

Related

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.

Uncaught SyntaxError: Unexpected token :

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.

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

JSONP:Hitting url and parsing the response

I have url which will return response as Content=image|text=hello|.
While hitting the url with AJAX i am getting Cross domain issues.
So i tried searching for jsonp.But unable to do that.I am able to hit the url using script.
But unable to catch the response into string.My final aim is to hit the url and catch the response in some var x.So i want x=Content=image|text=hello| after hitting the url.
I used ajax it works in IE only.
Please try to help/advice to do the same with jsonp.
You can't do what you've described from a browser doing without Cross-Origin Resource Sharing, which would require that the server supplying the response trusts your document's origin, that the user is using a CORS-enabled browser, and that if you want to support IE8 and IE9, your code handles using the MS-specific XDomainRequest object rather than the standard XMLHttpRequest object on those two browsers (other browsers support CORS via XMLHttpRequest, including IE10 and up).
If you can get the source of this information to reply with JSONP instead, you can do it without CORS. JSONP is a specific format. Here's example of that data in JSONP format:
foo({"Content":"image","text":"hello"})
...assuming you've called it with a URL like http://example.com/some/api?callback=foo (with JSONP, you supply the name of the function — foo in the above — in the query string).

Categories

Resources