jQuery Ajax vs Classic ASP returning data
Help would be appreciated.
In My ASP page I am returning this [{'id': '123'}]
. If you view the source then that is all you will see on the page nothing else. So makes sense that when I do a get that I would be able to get the id.
My Code (which I know works because I use it to do a get on flat files containing the same data. All I want to do now is create the data on the fly):
$.ajax({
type: "GET",
url: "http://localhost/GetCustNewID.asp?callback=?",
async: false,
dataType: "jsonp",
success: function(data){
$(data).each(function(){
alert(this.id)
});开发者_如何转开发
},
error:function(x, e){
if (x.status === 0){
alert("You are offline.");
}
else if(x.status === 404){
alert("404 file not found error");
}
else if(x.status === 500){
alert("500 internal server error");
}
else if(e === 'parsererror'){
alert("200 but can't parse json response");
}
else if(e === 'timeout'){
alert("Request timed out.");
}
else {
alert("Unknown AJAX error");
}
}
});
I get a "200 but can't parse json response"
returned. Is there something I am missing?
Thank you.
You are returning (invalid) JSON, but your JS is looking for JSON-P.
A JSON-P resource would be:
Content-Type: application/javascript
value_of_callback_query_string_value([{"id": "123"}]);
As opposed to a JSON response:
Content-Type: application/json
[{"id": "123"}]
Note that in JSON, strings are delimited with double quotes. You should make use of JSON Lint to test your output, and use a library (some are listed near the end of the JSON homepage) instead of trying to craft JSON by hand.
Seems like you should be using "json" instead of "jsonp".
精彩评论