2

I have a simple ajax call:

$.ajax({
    url: 'http://localhost:39657/List/Receptacle',
    dataType: "json",
    success: function(json) { alert("success"); }
});

And in Fiddler, the entire response is:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Sat, 18 Feb 2012 07:39:11 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 97
Cache-Control: private
Content-Type: application/json; charset=utf-8
Connection: Close

[{"Selected":false,"Text":"ABC","Value":"3"},{"Selected":false,"Text":"XYZ","Value":"4"}]

So it appears to have worked, but I get no alert...

What am I doing wrong here? Why is my JSON not parsing? Thank you.

5
  • What is the URL that has been called? Please post the whole URL, you may be accessing incorrect one. Commented Feb 18, 2012 at 7:38
  • Tadeck, the url is fine, I am watching the response come back in Fiddler. Commented Feb 18, 2012 at 7:41
  • What version of jquery are you use? Day or two ago I have strange behavior like this using last Jquery version Commented Feb 18, 2012 at 7:53
  • Why not try to use getJSON? Commented Feb 18, 2012 at 7:58
  • @neoascetic: getJSON() is only a shorthand for get() with JSON response format. Commented Feb 18, 2012 at 16:11

2 Answers 2

2

why you have '?callback=?' at the end of url ? is it jsonp? yes then change the dataType:'jsonp'

Edit

make a error handler

$.ajax({
    url: 'localhost:39657/List/' + $(this).val(),
    dataType: "json",
    success: function(json) {
           alert("success");
    },
    error:function(xhr){
        console.log(xhr.responseText);
    }
});

see if it hits the success handler or the error handler and what error message does it give? i dont think there is something wrong with the json parsing as it is valid json and being parsed fine look here


as infered from the comments you are running into the CORS blues, which is implemented as a security feature to prevent cross site scripting attacks you cannot use jsonp by just setting the dataType to jsonp on the client side, the server side has to be configured as well, if its a web-service you can set the response header to allow the cross domain resource sharing by

var resp = new HttpResponseMessage();
resp.Headers.Add("Access-Control-Allow-Origin","*");

alternatively you can make a server side proxy and have that proxy call your other project whether its a webservice or web application and then return the received response to the client side

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

8 Comments

I updated it (edit reflects the changes) and still do not get the alert, the response is the same.
it is hitting the error handler, but xhr.responseText is simply an empty string.
something is not right there is not a problem with the json parsing i am suspecting you are running in the cross domain issues ... try accessing the same url from the browser address bar see if it returns the result, also try with the url /List/+$(this).val() remove the localhost and port
url works fine in the browser - right now I even have a hardcoded url in the (updated in the explanation above). I am watching fiddler and the request is exactly as above.
they are separate projects, so they are running on separate ports - that's why I thought I may need jsonp?
|
0

you have to do this serverside:

 HttpResponseMessage m = new HttpResponseMessage(HttpStatusCode.NotFound);            
 m.Content = new StringContent("oops");                             
 throw new HttpResponseException(m);

1 Comment

Please explain why. The OP asked what he did wrong and why his JSON is not parsing. Just source code is not enough.

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.