【问题标题】:SyntaxError: invalid label while retrieving JSON objectSyntaxError:检索 JSON 对象时标签无效
【发布时间】:2012-08-06 14:52:35
【问题描述】:

在检索 JSON 对象时,我收到以下错误:

  1. 语法错误:Mozilla 中的标签无效。
  2. Uncaught SyntaxError: Unexpected token : in Chrome

我的 JSON 对象是如下所示的格式:

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

代码:

<!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 = "Welcome_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"); }).complete(function() { alert("complete"); });
    jqxhr.complete(function(){ alert("second complete"); });
 }
 </script>
</head>
<body>
<input id="jsonpbtn2" type="submit" value="button" />
</body>
</html>

【问题讨论】:

  • 您告诉 jQuery 期待 JSONP,但响应看起来像 JSON,而不是 JSONP。响应被评估为 JavaScript,但由于 JSON 不是有效的 JavaScript,您会收到该错误。您必须设置服务器以返回 JSONP。
  • 您能否通过设置 server 返回 jsonp 来澄清您的意思。其实json、jsonp我都试过了。
  • 答案在我链接到的问题中:stackoverflow.com/a/2816696/218196。我的意思是您必须配置/编程您的服务器以返回 JSONP,而不是(仅)JSON。如果您不知道 JSONP 是什么,请查看这篇 Wikipedia 文章:en.wikipedia.org/wiki/JSONP
  • 我只需要 JSON 对象而不需要 JSONP。请您清除它。

标签: javascript jquery json


【解决方案1】:

你可以让代码看起来像这样

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) {


            }

【讨论】:

    【解决方案2】:

    嘿,试试这个你正在使用的网址

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

    这样使用

    【讨论】:

      【解决方案3】:

      我在调用 asmx Web 服务 (.NET) 时遇到了完全相同的问题。

      我通过用方括号括起来我的返回值来解决它,如下所示:

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

      然后因为讨厌的 d 参数“评估”了我的 return var:

      $.ajax({
                  type: "POST",
                  url: 'http://www.example.com/',
                  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) {
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-30
        • 1970-01-01
        • 1970-01-01
        • 2010-09-23
        • 1970-01-01
        • 1970-01-01
        • 2012-04-11
        相关资源
        最近更新 更多