5

Got a simple autocomplete box (jquery ui) that gets its source from a web service. The code is something like below:

var autocompleteOptions = {
    source = getDataFromService,
    minLength: 3
};

var getDataFromService = function(request, response) {
    var ajaxOptions = {
        url: "http://myservice:1234/somedata/",
        dataType: "jsonp",
        data: "someVariable = " + request.term,
        success: function(data) { alert("data"); },
        error: function(xhr, description, error) { alert("failed"); }
    };

    $.ajax(ajaxOptions);
}

$(someSelector).autocomplete(autocompleteOptions);

Looking in fiddler and even in the Firefox firebug panel, I can see that the JSON is correctly returned, and the server response is a 200. I have even checked the created jsonp script snippet, which also contains the correct JSON. However it always hits the error function not the success one.

I have also tried using complete and getting the data from the xhr manually, however the responseText and responseXml are both undefined. The error contained says parse error, but it all seems to be syntactically correct json, as the firebug panel and fiddler both display it fine.

HTTP/1.1 200 OK
Server: ASP.NET Development Server/9.0.0.0
Date: 28 Jun 2011 11:17:04 GMT
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 2.0
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 29
Connection: Close

[{"id":"1", "somevar":"hello"}]
4
  • 1
    Can you post the JSON in question? If you're getting a "parse error", there must be a reason ... Commented Jun 28, 2011 at 14:43
  • Will add the raw output to my original post if it helps Commented Jun 28, 2011 at 14:45
  • What are you seeing in the alert box when the AJAX call completes? Or is it not alerting? Commented Jun 28, 2011 at 15:10
  • Just says "error" although I am sure one time I saw something about "parse error" but it wasnt clear what was the actual problem as the JSON is valid and the response is fine... Commented Jun 28, 2011 at 21:40

1 Answer 1

5

That JSON is not correct,

[{"id":"1", somevar:"hello"}]

needs to be

[{"id":"1", "somevar":"hello"}]

JSON requires double quotes.

http://jsfiddle.net/robert/Y6ypV/

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

Take From: http://www.json.org/

Sign up to request clarification or add additional context in comments.

3 Comments

sorry typo, was just copy typing from other machine, it does correctly contain the quotations around the data on the other machine. Will update my post.
I didnt know there was a jsonlint, I am using jslint in my build pipeline... will go check the json in there now... EDIT Valid json
Ends up that because we were using JsonP we had to mod the service providing the information to wrap the returned data up in a callback. As the service didnt support JsonP (and no where seems to tell you that you need to have the server support it) it couldnt complete the request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.