最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SyntaxError: invalid label while retrieving JSON object - Stack Overflow

programmeradmin2浏览0评论

When retrieving JSON object I receive the following error:

  1. SyntaxError: invalid label in Mozilla.
  2. Uncaught SyntaxError: Unexpected token : in Chrome

My JSON object is the format shown below:

{
   "userName" : "clevermeal835",
   "userRole" : "Participant",
   "userAccountStatus" : "Active"
}

Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="Scripts/jquery-min.js"></script>
<script src="Scripts/jquery_validate.js"></script>
<script>
$(document).ready(function() {
    loadLatestTweet();  
});

function loadLatestTweet(){
var xhr = new XMLHttpRequest();
var uid = "clevermeal835";
var pwd = "Wele_1";
var userType = "participant";
    var surl =      'http://localhost:8080/RESTlet_WS/MobiSignIn/{"userName":"'+uid+'","password":"'+pwd+ '","userType":"'+userType+'"}&callback=?';

    var jqxhr = $.getJSON(surl, function() {
        alert("success");
    }).success(function() { alert("second success");   }).error(function() { alert("error"); })plete(function() { alert("plete"); });
    jqxhrplete(function(){ alert("second plete"); });
 }
 </script>
</head>
<body>
<input id="jsonpbtn2" type="submit" value="button" />
</body>
</html>

When retrieving JSON object I receive the following error:

  1. SyntaxError: invalid label in Mozilla.
  2. Uncaught SyntaxError: Unexpected token : in Chrome

My JSON object is the format shown below:

{
   "userName" : "clevermeal835",
   "userRole" : "Participant",
   "userAccountStatus" : "Active"
}

Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="Scripts/jquery-min.js"></script>
<script src="Scripts/jquery_validate.js"></script>
<script>
$(document).ready(function() {
    loadLatestTweet();  
});

function loadLatestTweet(){
var xhr = new XMLHttpRequest();
var uid = "clevermeal835";
var pwd = "Wele_1";
var userType = "participant";
    var surl =      'http://localhost:8080/RESTlet_WS/MobiSignIn/{"userName":"'+uid+'","password":"'+pwd+ '","userType":"'+userType+'"}&callback=?';

    var jqxhr = $.getJSON(surl, function() {
        alert("success");
    }).success(function() { alert("second success");   }).error(function() { alert("error"); }).plete(function() { alert("plete"); });
    jqxhr.plete(function(){ alert("second plete"); });
 }
 </script>
</head>
<body>
<input id="jsonpbtn2" type="submit" value="button" />
</body>
</html>
Share Improve this question edited Aug 9, 2012 at 12:43 Miika L. 3,3531 gold badge26 silver badges36 bronze badges asked Aug 9, 2012 at 11:30 paripurnaparipurna 1352 silver badges10 bronze badges 8
  • 2 You tell jQuery to expect JSONP but the response looks like JSON, not JSONP. The response is evaluated as JavaScript, but since JSON is not valid JavaScript, you get that error. You have to set up your server to return JSONP. – Felix Kling Commented Aug 9, 2012 at 11:31
  • 1 possible duplicate of Invalid Label Error with JSON request – Felix Kling Commented Aug 9, 2012 at 11:32
  • can you please clarify what you mean by setting server to return jsonp . Actually i have tried both json, jsonp. – paripurna Commented Aug 9, 2012 at 11:37
  • The answer is in the question I linked to: stackoverflow./a/2816696/218196. I mean that you have to configure/program your server to return JSONP, not (only) JSON. If you don't know what JSONP is, then have a look at this Wikipedia article: en.wikipedia/wiki/JSONP. – Felix Kling Commented Aug 9, 2012 at 11:41
  • I need only JSON object not JSONP.Can you please clear it. – paripurna Commented Aug 9, 2012 at 11:46
 |  Show 3 more ments

3 Answers 3

Reset to default 3

you can make code seem like this

var params = { "Username": UserNameValue,"Password": PassValue};


        $.ajax({
            type: "POST",
            url: "http://localhost:8080/RESTlet_WS/MobiSignIn/",
            contentType: 'application/json',

            data: JSON.stringify(params),
            dataType: 'json',
            async: false,
            cache: false,
            success: function (response) {


            },
            error: function (ErrorResponse) {


            }

Hey try this for the surl you are using

var params = encodeURIComponent('{"userName":"'+uid+'","password":"'+pwd+ '","userType":"'+userType+'"}');
var surl = 'http://localhost:8080/RESTlet_WS/MobiSignIn?params='+params+'&callback='

Use like this

I had the exact same problem when calling an asmx web service (.NET).

I resolved it by enclosing my return value with square brackets like so:

return @"[ {{ ""status"" : ""OK"", ""message"" : ""Success !!!"" }} ]";

and then "evaled" my return var because of the pesky d param:

$.ajax({
            type: "POST",
            url: 'http://www.example./',
            contentType: 'application/json; charset=utf-8',
            data: '{name: "' + vName + '", target: "' + vTarget + '", phone: "' + vPhone + '", timeframe: ' + true + '}',
            dataType: 'json',
            success: function (msg) {

                jsonMsg = eval(msg.d);

                alert(jsonMsg.status);
                alert(jsonMsg.message);
            },
            error: function (xhr, msg) {
            }
        });
发布评论

评论列表(0)

  1. 暂无评论