views:

101

answers:

5
       $.ajax({
  beforeSend: function(xhr) {
 xhr.setRequestHeader('Authorization', "Basic YWRtaW46YWRtaW4=");        
},
   url: "https://test.com/incident.do?JSON&callback=?&sysparm_action=getRecords",
   dataType: "json",
   contentType: "application/json",
   method: 'GET',
   success: function(a,b,c) { 
    alert(a);
         }          
 });

I call this function with a button press... Firebug shows that I get this JSON response (which I know is valid) [truncated for clarity]

{
    "records": [
        {
            "service_offering": "",
            "number": "INC0000009"
        },
        {
            "service_offering": "",
            "number": "INC0000010"
        }
]
}

Firebug shows the error "invalid label https://test.com/incident.do?JSON&callback=jsonp1279049933243&sysparm_action=getRecords Line 1

How can I fix this? Thanks!

+2  A: 

You may want to verify that the service supports jsonp. If not you'll need to set up a server side proxy.

http://stackoverflow.com/questions/1230897/using-jquery-to-get-json-data-returns-invalid-label-error

brendan
How can I check to make sure that the server supports it? If I run my script on the same domain can I avoid these issues?
imagineblue
to support jsonp it needs to return the json wrapped in a javascript function.
redsquare
Good call Brendan, that's most likely exactly what the issue is.
lark
A: 

You say that you know its valid. Can you funnel it into the parseJSON function and see if you have any luck with that?

$.parseJSON(str_json);

Note: You haven't mentioned which version of jQuery you're using. This method is available only in jQuery 1.4.1 and above.

lark
The problem is that I can't work with the results because the success function is never called...I validated the JSON here to make sure http://www.jsonlint.com/
imagineblue
I'm trying to help... Grab the JSON string and use an interpreter with jQuery to test. Firebug is a good console debugger.jQuery.parseJSON( your_string ); jQuery does not communicate with json lint to approve valid JSON, so that's not very relevant.
lark
A: 

Are you sure the json is valid? Check it validates at JSONLint.

redsquare
JSONLint confirms that the json is valid. I've looked around and some people fix this problem by adding an extra set of parentheses around the data but the problem is that I can't work with the results.
imagineblue
A: 

Your response is JSON, which is valid, but that's not what jQuery's looking for. When you specify &callback=? in the URL, jQuery is expecting a JSONP response, which looks different, your response should be

jsonp1279049933243({
  "records": [
    {
        "service_offering": "",
        "number": "INC0000009"
    },
    {
        "service_offering": "",
        "number": "INC0000010"
    }
  ]
});

What happens when you specify callback=? is that jQuery generates a name for your success function, in this case jsonp1279049933243, JSONP works by just generating a <script> tag in the page, so when it fetches that url, it's really just including a JavaScript file, running a function, but instead of this:

<script type="text/javascript">
  jsonp1279049933243({ "records": [....] });
</script>

What's effectively happening now is:

<script type="text/javascript">
  { "records": [....] }
</script>

...which isn't valid JavaScript. Now of course it's loaded via src=https://test.com/incident.do?JSON&amp;callback=jsonp1279049933243&amp;sysparm_action=getRecords, but the invalid syntax/label error is the same.

Nick Craver
Thanks, so the reason I'm including callback=? is that without it I'm getting a null response back, with a 200 OK...
imagineblue
@bluemango - That's correct...you have to use JSONP on a *remote* domain, the `<script>` tag approach is a GET, not an XmlHttpRequest, which would be blocked due to the same-origin policy.
Nick Craver
A: 

try to get the data as a plain text and convert it into json on client side. I use "http://www.json.org/json2.js" script to do this. It helped me to resolve "invalid label" problem in my application. (I mentioned the script because eval() function didn't help, no idea why...)

good luck :)

vadimk