2012-08-09 56 views
6

當檢索JSON對象,我收到以下錯誤:語法錯誤:無效的標籤,同時檢索JSON對象

  1. 語法錯誤:在Mozilla無效的標籤。
  2. 未捕獲的SyntaxError:意外的標記:在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> 
+2

你告訴了jQuery期待JSONP,但響應看起來像JSON,而不是JSONP。響應評估爲JavaScript,但由於JSON不是有效的JavaScript,因此會出現該錯誤。你必須設置你的服務器來返回JSONP。 – 2012-08-09 11:31:52

+1

[JSON請求無效標籤錯誤]的可能的重複(http://stackoverflow.com/questions/2816653/invalid-label-error-with-json-request) – 2012-08-09 11:32:13

+0

可以請你澄清你的意思是通過設置服務器返回jsonp。其實我已經嘗試了json,jsonp。 – paripurna 2012-08-09 11:37:11

回答

3

可以使代碼看起來像這樣

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


      } 
0

嘿嘗試一下本作的SURL您使用

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

使用這樣

0

調用一個ASMX Web服務(.NET),當我有完全相同的問題。

我解決它通過內附我用方括號像這樣的返回值:

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

,然後「evaled」因爲討厭d PARAM的我回來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) { 
      } 
     });