6

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 = "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>
Miika L.
  • 3,245
  • 1
  • 22
  • 35
paripurna
  • 135
  • 1
  • 10
  • 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 Aug 09 '12 at 11:31
  • 1
    possible duplicate of [Invalid Label Error with JSON request](http://stackoverflow.com/questions/2816653/invalid-label-error-with-json-request) – Felix Kling Aug 09 '12 at 11:32
  • can you please clarify what you mean by setting server to return jsonp . Actually i have tried both json, jsonp. – paripurna Aug 09 '12 at 11:37
  • The answer is in the question I linked to: http://stackoverflow.com/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: http://en.wikipedia.org/wiki/JSONP. – Felix Kling Aug 09 '12 at 11:41
  • I need only JSON object not JSONP.Can you please clear it. – paripurna Aug 09 '12 at 11:46
  • It looks your are making a cross-domain request, since you are making the request to port `:8080`. If so, you can only use JSONP or set the correct headers for [CORS](https://developer.mozilla.org/en-US/docs/HTTP_access_control). See also this question: http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain. If that's not the case and the page is in fact served from the same domain, remove `callback=?` from the URL so that jQuery does not think it's JSONP. This is all described in the documentation, I recommend to read it. – Felix Kling Aug 09 '12 at 11:52
  • If I use callback=? in URL I can see response in console but when I tried by removing callback=? I cannot see any response in console. I am new to Jquery. Can you please help me. – paripurna Aug 09 '12 at 11:58
  • I am already helping you all the time. It seems you are making a cross-domain request. So the first part of my previous comment applies. Set up your server to return JSONP. For that, learn what JSONP is, I provided many links already, read them! Or set the correct headers for CORS for which I provided a link as well. Read it! The problem is with your server, not your JavaScript code, so you have to fix it by yourself. Read all the links I posted. Good luck! – Felix Kling Aug 09 '12 at 12:02

3 Answers3

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


            }
Jignesh.Raj
  • 5,547
  • 4
  • 24
  • 54
0

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

AmGates
  • 1,941
  • 15
  • 28
0

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.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) {
            }
        });
Ted
  • 3,761
  • 1
  • 15
  • 30