admin管理员组

文章数量:1315942

I am having the following jQuery script.

And getting the error:

SyntaxError: Unexpected end of input

My js file is correct on syntax side, do not missing any closing or opening bracket. Thanks for any suggestion.

I am having the following jQuery script.

http://www.codeshare.io/aQxBp

And getting the error:

SyntaxError: Unexpected end of input

My js file is correct on syntax side, do not missing any closing or opening bracket. Thanks for any suggestion.

Share Improve this question asked Apr 1, 2015 at 20:20 Richard ZilahiRichard Zilahi 6922 gold badges12 silver badges25 bronze badges 9
  • 1 Try removing the "codeshare.io/new" from }http://www.codeshare.io/new – KJ Price Commented Apr 1, 2015 at 20:23
  • ah wel, it seems i do not get your point. you entered http://www.codeshare.io/new should i remove what again, please? – Richard Zilahi Commented Apr 1, 2015 at 20:28
  • It appears your code is working now. You had that url unmented in your code. That would cause your error. – KJ Price Commented Apr 1, 2015 at 20:32
  • btw, there is no problem with the if includes the #emailtoinvite the problem is still occurs when that code snippet is removed. – Richard Zilahi Commented Apr 1, 2015 at 20:32
  • actually that url was not added by me. idk how is that got it there. :) check it now: codeshare.io/aQxBp – Richard Zilahi Commented Apr 1, 2015 at 20:34
 |  Show 4 more ments

2 Answers 2

Reset to default 6

The here is that you are telling your ajax call to expect to receive JSON. When JSON is not received, an error will be thrown when JSON.parse tries to parse a non-JSON string. JSON is fairly simple in nature but you have to be intentional. To make an ajax call similar to this (note response.emailtoinvite):

jQuery.ajax({
    type: "POST", // HTTP method POST or GET
    url: "sendinvitation.php", //Where to make Ajax calls
    dataType: "json", // Data type, HTML, json etc.
    data: myData, //Form variables
    success: function(response) {
        alert(response.emailtoinvite);
    },
    error: function(xhr, ajaxOptions, thrownError) {
        //$(".btn.btn-primary").show(); //show submit button
        alert(thrownError);
    }

});

....sendinvitation.php will have to send back a proper JSON string. The following works:

{emailtoinvite: "[email protected]", idToInvite: 136}

Important things to note here, all strings must be wrapped in double quotation marks "". Also, the entire string must be wrapped in {}. numbers can be left without quotes around them.

Also important to note that the above is not proper syntax, but does sometimes work. Proper syntax also requires you to wrap you "keys" in quotes "" as well:

{"emailtoinvite": "[email protected]", "idToInvite": 136}
$.ajax({
            type: 'POST',
            dataType: 'JSON',
            url:"test.php",//<?php echo $_GET['url'] ?>,
            data: data,
            success: function(response) {
                if ($('#toggle').prop('checked')) {
                    $('.led-<?php echo $_GET[\'color\'] ?>').show();
                    $('.led-off').hide();
                } else {
                    $('.led-<?php echo $_GET[\'color\'] ?>').hide();
                    $('.led-off').show();
                }
            },
        });

本文标签: javascriptUnexpected end of input jquery using ajaxStack Overflow