Syntax Error: invalid label when retrieving a JSON object

When retrieving a JSON object, the following error appears:

  • Syntax Error: Invalid label in Mozilla.
  • Uncaught SyntaxError: Unexpected token: in Chrome

My JSON object is the format shown below:

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

the 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 = "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> 
+6
source share
3 answers

you can create code similar to 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) { } 
+3
source

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 it as

0
source

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

I resolved it by including my return value with square brackets like this:

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

and then "evaled" my return var due to pesky d param:

 $.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) { } }); 
0
source

Source: https://habr.com/ru/post/922463/


All Articles