Rectangle 27 122

You've told jQuery to expect a JSONP response, which is why jQuery has added the callback=jQuery16406345664265099913_1319854793396&_=1319854793399 part to the URL (you can see this in your dump of the request).

What you're returning is JSON, not JSONP. Your response looks like

{"red" : "#f00"}
jQuery16406345664265099913_1319854793396({"red" : "#f00"})

If you actually need to use JSONP to get around the same origin policy, then the server serving colors.json needs to be able to actually return a JSONP response.

If the same origin policy isn't an issue for your application, then you just need to fix the dataType in your jQuery.ajax call to be json instead of jsonp.

XMLHttpRequest cannot load isohunt.com/js/json.php?ihq=test. Origin localhost:8888 is not allowed by Access-Control-Allow-Origin.

Okay. The Weather Underground URL you gave does respond to JSONP requests. (You'll see this if you add ?callback=something to that URL. The error you're getting when you use dataType: json is because of the same origin policy. Let me add to my answer.

The issue really seems to be if the server responds with Type: text/javascript, then Chrome doesn't bark about Uncaught SyntaxError. If the remote server returns as type: text/plain or application/json then Chrome reports the Uncaught SyntaxError.

jquery - json Uncaught SyntaxError: Unexpected token : - Stack Overflo...

jquery ajax json syntax-error
Rectangle 27 122

You've told jQuery to expect a JSONP response, which is why jQuery has added the callback=jQuery16406345664265099913_1319854793396&_=1319854793399 part to the URL (you can see this in your dump of the request).

What you're returning is JSON, not JSONP. Your response looks like

{"red" : "#f00"}
jQuery16406345664265099913_1319854793396({"red" : "#f00"})

If you actually need to use JSONP to get around the same origin policy, then the server serving colors.json needs to be able to actually return a JSONP response.

If the same origin policy isn't an issue for your application, then you just need to fix the dataType in your jQuery.ajax call to be json instead of jsonp.

XMLHttpRequest cannot load isohunt.com/js/json.php?ihq=test. Origin localhost:8888 is not allowed by Access-Control-Allow-Origin.

Okay. The Weather Underground URL you gave does respond to JSONP requests. (You'll see this if you add ?callback=something to that URL. The error you're getting when you use dataType: json is because of the same origin policy. Let me add to my answer.

The issue really seems to be if the server responds with Type: text/javascript, then Chrome doesn't bark about Uncaught SyntaxError. If the remote server returns as type: text/plain or application/json then Chrome reports the Uncaught SyntaxError.

jquery - json Uncaught SyntaxError: Unexpected token : - Stack Overflo...

jquery ajax json syntax-error
Rectangle 27 17

You need quotes for strings. For example, you need arr[0][0]='sathis'; instead of arr[0][0]=sathis;

arr[0] = ['sathis', 'sathis@gmail.com', 'namakkal', 21];

I just had the same ILLEGAL error but it appears to be from pasting text into Coda and allowing it to change the UTF format. Needed to type the same code by hand and it was fine.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

jquery - Uncaught SyntaxError: Unexpected token ILLEGAL - Stack Overfl...

jquery token
Rectangle 27 2

Web API does not support JSONP out of the box. You would need a JSONP media-type formatter. (The media-type formatter is the object that serializes the data to a particular format, such as JSON or XML.)

The reason you are getting back JSON when you asked for JSONP is that when Web API cannot match the type that you requested, it returns the first format in its list, which by default is JSON.

jquery - ASP.NET Web API error “Uncaught SyntaxError: Unexpected token...

jquery asp.net-web-api
Rectangle 27 2

Web API does not support JSONP out of the box. You would need a JSONP media-type formatter. (The media-type formatter is the object that serializes the data to a particular format, such as JSON or XML.)

The reason you are getting back JSON when you asked for JSONP is that when Web API cannot match the type that you requested, it returns the first format in its list, which by default is JSON.

jquery - ASP.NET Web API error “Uncaught SyntaxError: Unexpected token...

jquery asp.net-web-api
Rectangle 27 2

Web API does not support JSONP out of the box. You would need a JSONP media-type formatter. (The media-type formatter is the object that serializes the data to a particular format, such as JSON or XML.)

The reason you are getting back JSON when you asked for JSONP is that when Web API cannot match the type that you requested, it returns the first format in its list, which by default is JSON.

jquery - ASP.NET Web API error “Uncaught SyntaxError: Unexpected token...

jquery asp.net-web-api
Rectangle 27 8

What makes you think that rubygems.org supports JSONP at all? I don't see any mention of JSONP in the documentation and when I do this:

I get the same plain old JSON as I do from

lynx -dump -source 'http://rubygems.org/api/v1/gems/rails.json'

The only difference between the two is the downloads and version_downloads change but that's to be expected.

When you use jsoncallback=? in the query string, jQuery will set up a callback function and assume that the remote URL will send back JavaScript (not JSON!) that will call the specified function. So, if the remote service sends back JSON when you're expecting JavaScript, the browser will end up trying to interpret the JSON as JavaScript and get upset because

{"dependencies":{"runtime":[{"name":"action ...

is not a valid JavaScript statement. This sounds exactly like the error you're seeing.

I think you're going to have to proxy the JSON through your own server. You'll need a controller on your server that makes that makes the call to get the JSON and then simply echoes it back to your JavaScript, this will get your around both the lack of JSONP support and your cross domain problem in your client.

Hey man, really I'm not that good with all this JSON stuff, but I used to have the Access-Control-Allow-Origin error and they told me to add the JSONP options, to get around that error, but now I am stuck with this, problem. Do you know how to pass through the ccess-Control-Allow-Origin error without adding the JSONP?

@rogeliog: I've added some more explanation in an update. I think you're going to have to set up a simple proxy controller to get around the lack of JSONP and the cross domain request problem at the same time.

this all explains the error indeed, check the post below here (made by CD Sanchez) on how to fix this

javascript - Jquery getJSON Uncaught SyntaxError: Unexpected token : e...

javascript json jquery
Rectangle 27 8

There are a number of examples on the jQuery.post API page that you linked to. They're about 1/3 of the way down the page. The last example on the page shows passing the 'json' content type in this method.

Here's a sample of doing a jQuery post -- and this works in jsFiddle without issue:

$(function() {
    $.post("/ajax_json_echo/", {html: "stuff"}, function(data) { alert(data.post_response.html);}, "json");
});

I'm passing my data to the service using 'json' as my data type.

So, are you sure there isn't a server side error happening. Try running your call through Fiddler or Firebug and see what you get back. Also, what type of data is your service expecting?

Actually when I did json, it didn't work. But the error went away when I did html. Which makes a lot of sense, since data is not json, I had to do parseJSON to make it into a js object. But the problem is that I thought the server return was json... return drupal_json($result); This is the code for the server side return. I mean thank you, that showed me where to put the datatype and answers the question, I'm still a little confused but thanks.

javascript - jquery Uncaught SyntaxError: Unexpected token : - Stack O...

javascript jquery ajax google-chrome
Rectangle 27 8

There are a number of examples on the jQuery.post API page that you linked to. They're about 1/3 of the way down the page. The last example on the page shows passing the 'json' content type in this method.

Here's a sample of doing a jQuery post -- and this works in jsFiddle without issue:

$(function() {
    $.post("/ajax_json_echo/", {html: "stuff"}, function(data) { alert(data.post_response.html);}, "json");
});

I'm passing my data to the service using 'json' as my data type.

So, are you sure there isn't a server side error happening. Try running your call through Fiddler or Firebug and see what you get back. Also, what type of data is your service expecting?

Actually when I did json, it didn't work. But the error went away when I did html. Which makes a lot of sense, since data is not json, I had to do parseJSON to make it into a js object. But the problem is that I thought the server return was json... return drupal_json($result); This is the code for the server side return. I mean thank you, that showed me where to put the datatype and answers the question, I'm still a little confused but thanks.

javascript - jquery Uncaught SyntaxError: Unexpected token : - Stack O...

javascript jquery ajax google-chrome
Rectangle 27 8

There are a number of examples on the jQuery.post API page that you linked to. They're about 1/3 of the way down the page. The last example on the page shows passing the 'json' content type in this method.

Here's a sample of doing a jQuery post -- and this works in jsFiddle without issue:

$(function() {
    $.post("/ajax_json_echo/", {html: "stuff"}, function(data) { alert(data.post_response.html);}, "json");
});

I'm passing my data to the service using 'json' as my data type.

So, are you sure there isn't a server side error happening. Try running your call through Fiddler or Firebug and see what you get back. Also, what type of data is your service expecting?

Actually when I did json, it didn't work. But the error went away when I did html. Which makes a lot of sense, since data is not json, I had to do parseJSON to make it into a js object. But the problem is that I thought the server return was json... return drupal_json($result); This is the code for the server side return. I mean thank you, that showed me where to put the datatype and answers the question, I'm still a little confused but thanks.

javascript - jquery Uncaught SyntaxError: Unexpected token : - Stack O...

javascript jquery ajax google-chrome
Rectangle 27 19

If you are using jsonp then the syntax is wrong

myJsonMethod({"id":3,"login":"pepe","key":"0D1DBA4BE87E02D43E082F9AA1ECFDEB"});
jsonp: false,
jsonpCallback: "myJsonMethod"
$.ajax({
    type : "Get",
    url :"http://XXXZZZ/Servlet/Login",
    data :"login="+login+"&password="+pass,
    dataType :"jsonp",
    jsonp: false,
    jsonpCallback: "myJsonMethod",
    success : function(data){
        alert(data);},
    error : function(httpReq,status,exception){
        alert(status+" "+exception);
    }
});
success
static JSONObject Login(String login, String password) throws JSONException{
static String Login(String login, String password){         ...         json.put("id", id);         json.put("login", login);         json.put("key", key);         return("myJsonMethod("+json+");");

@PabloPostigo, you can use the JSONPObject class

jquery - $.ajax and JSONP. ParseError and Uncaught SyntaxError: Unexpe...

jquery ajax json
Rectangle 27 2

I have spent the last few days trying to figure this out myself. Using the old json dataType gives you cross origin problems, while setting the dataType to jsonp makes the data "unreadable" as explained above. So there are apparently two ways out, the first hasn't worked for me but seems like a potential solution and that I might be doing something wrong. This is explained here [ https://learn.jquery.com/ajax/working-with-jsonp/ ].

The one that worked for me is as follows: 1- download the ajax cross origin plug in [ http://www.ajax-cross-origin.com/ ]. 2- add a script link to it just below the normal jQuery link. 3- add the line "crossOrigin: true," to your ajax function.

$.ajax({
      crossOrigin: true,
      url : "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.86,151.195&radius=5000&type=ATM&keyword=ATM&key=MyKey",
      type : "GET",
      success:function(data){
         console.log(data);
      }
    })

thanks S . Rehan. This post was a lifesaver!

jquery - json Uncaught SyntaxError: Unexpected token : - Stack Overflo...

jquery ajax json syntax-error
Rectangle 27 2

I have spent the last few days trying to figure this out myself. Using the old json dataType gives you cross origin problems, while setting the dataType to jsonp makes the data "unreadable" as explained above. So there are apparently two ways out, the first hasn't worked for me but seems like a potential solution and that I might be doing something wrong. This is explained here [ https://learn.jquery.com/ajax/working-with-jsonp/ ].

The one that worked for me is as follows: 1- download the ajax cross origin plug in [ http://www.ajax-cross-origin.com/ ]. 2- add a script link to it just below the normal jQuery link. 3- add the line "crossOrigin: true," to your ajax function.

$.ajax({
      crossOrigin: true,
      url : "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.86,151.195&radius=5000&type=ATM&keyword=ATM&key=MyKey",
      type : "GET",
      success:function(data){
         console.log(data);
      }
    })

thanks S . Rehan. This post was a lifesaver!

jquery - json Uncaught SyntaxError: Unexpected token : - Stack Overflo...

jquery ajax json syntax-error
Rectangle 27 10

succes : function(data){

That's a typo:

success : function(data){

No, sorry i wrote it wrong here, in the code its written "success"

jquery - $.ajax and JSONP. ParseError and Uncaught SyntaxError: Unexpe...

jquery ajax json
Rectangle 27 1

You can't just use JSONP, it has to be supported by the server. JSONP is a hack where the server wraps your JSON data in a javascript function (the callback). When jQuery received it, it evaluates it and tries to run the function. If the server you're calling doesn't do the wrapping, it won't work. Hence the token errors.

If you have control over the server you can either implement JSONP support or provide proper cross domain support for your AJAX calls. This is commonly referred to as CORS (cross origin resource sharing).

You can make your CORS settings more permissive by modifying your access policies. There are plenty of tutorials on how to do this with virtually any web server as well as in virtually any language.

javascript - Uncaught SyntaxError: Unexpected token : jQuery ajax - St...

javascript jquery json ajax
Rectangle 27 1

You can't just use JSONP, it has to be supported by the server. JSONP is a hack where the server wraps your JSON data in a javascript function (the callback). When jQuery received it, it evaluates it and tries to run the function. If the server you're calling doesn't do the wrapping, it won't work. Hence the token errors.

If you have control over the server you can either implement JSONP support or provide proper cross domain support for your AJAX calls. This is commonly referred to as CORS (cross origin resource sharing).

You can make your CORS settings more permissive by modifying your access policies. There are plenty of tutorials on how to do this with virtually any web server as well as in virtually any language.

javascript - Uncaught SyntaxError: Unexpected token : jQuery ajax - St...

javascript jquery json ajax
Rectangle 27 6

There appears to be an "invisible" character at the end of this code:

$(".select a").click(function(event){
    event.preventDefault();
    $(".about_txt").hide('slow');
    var toShow = $(this).attr('href');
    $(toShow).show('slow');
});  //<= There's an invisible character here, 
     //    you can try pressing backspace once at the end of the semicolon

For quick fix, delete the invisible character OR copy&paste the following code and replace yours:

$(document).ready(function() {
    $(".select a").click(function(event){
        event.preventDefault();
        $(".about_txt").hide('slow');
        var toShow = $(this).attr('href');
        $(toShow).show('slow');
    });
});

Edit: A bit of investigation shows that the invisible character is ZERO WIDTH SPACE (U+200B)

You are the man! It works!!!

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

Wordpress jQuery Uncaught SyntaxError: Unexpected token ILLEGAL - Stac...

jquery wordpress syntax-error
Rectangle 27 6

The solution for this is to add a local proxy that your jQuery code will call. Your proxy will be server side code (PHP, Python, Ruby, etc) that forwards the query on to Valve and then returns it to your jQuery call. You will, however, have to use one of their supported formats (of which JSONP is not one).

A high level view of what you'll be doing:

  • Create PHP file that accepts the parameters jQuery will be passing. In this case, it looks like account ID and matches you want to receive. Do not pass the API key, this should be stored in the PHP file
  • In PHP, build your steamurl using the stored API key, and the two passed values
  • Issue a call to the Valve servers using this steamurl and retrieve the results.

Your PHP will look something like this (and should include more error checking since I am just taking the $_GET values as gospel:

$matches = $_GET['matches'];
$acct = $_GET['accountid'];
$APIKEY = <YOURKEYHERE>;

$steamurl = "https://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/V001/?key=$APIKEY&account_id=$acct&Matches_Requested=$matches&format=json";
$json_object= file_get_contents($steamurl);
header('Content-Type: application/json');
echo $json_object;

Now you can use jQuery to parse this JSON response.

javascript - Cross domain jquery ajax (Jsonp): Uncaught SyntaxError: U...

javascript jquery ajax json jsonp
Rectangle 27 6

angular.callbacks._0 (JSON)

the response is not being wrapped in angular.callback._0. For jQuery, the call is being wrapped correctly. This is the correct description for my issue.

jquery - JsonP returning "Uncaught SyntaxError: Unexpected token :" An...

jquery json angularjs jsonp angular-http
Rectangle 27 4

$('#selectable li:first').addClass('ui-selected');
.addClass('ui-selected');?

Also, you had a extra }.

$(document).ready(function() {
    $('#edit_patient_info').click(function () {
        //Get the data from all the fields

        $.ajax({
            url: "patient_info_controller.php", 
            type: "POST",
            data: data,     
            success: function (msg) {
                if (msg==1) {             
                    getPersoonlijkGegevens(user_id);
                    unLockFirstPage();
                    alert("Gegevens zijn gewijzigd!");
                    $("#searchbox").val(voornaam.val());
                    searchPatient();
                    $('#selectable li:first').addClass('ui-selected');
                }
            }      
        });
    });
});

That's still got a syntax error; you need a ); after that second to last } to complete the .click() function call. You've essentially done what I said in my answer, which you seem to think is wrong.

@AnthonyGrist: should be fixed now, not? You made no mention of the hidden character messing up his code.

That's because the hidden character wasn't visible in the question, and there was no reason for me to suspect one was there because there was an immediately obvious problem for the error being reported: the extra, unnecessary }. If he'd fixed that he would have been informed of the extra character by his debugging tool.

@AnthonyGrist: Uncaught SyntaxError: Unexpected token } is the only error you will ever get from that character, from some JS validators.

The problem was the hidden character. I have re-written everything and the problem is solved now, thank guys.

javascript - Uncaught SyntaxError: Unexpected token } - Stack Overflow

javascript jquery ajax token