我正在调用返回 json 对象的处理程序,但响应导致$.ajax()
调用在没有 responseText 的情况下命中错误函数。处理程序response.ContentType
是应用程序/json。
ajax调用...
$.ajax({
url: "http://localhost/_tools/ajax/getInstagramObject.ashx",
type: 'GET',
dataType: 'json',
success: function (msg) {
alert(msg.length);
},
error: function (e) {
alert(e.responseText);
}
});
这是处理程序返回的 json
{"info":[{"userName":"jamie_cahs","id":"42763829","name":"Jamie Rose","data":[{"images":{"lowResolution":{"url":"http://distilleryimage4.s3.amazonaws.com/de9c1e74bf4511e1abd612313810100a_6.jpg","width":306,"height":306},"thumbnail":{"url":"http://distilleryimage4.s3.amazonaws.com/de9c1e74bf4511e1abd612313810100a_5.jpg","width":150,"height":150},"standardResolution":{"url":"http://distilleryimage4.s3.amazonaws.com/de9c1e74bf4511e1abd612313810100a_7.jpg","width":612,"height":612}},"caption":{"text":"#boston"},"link":"http://instagr.am/p/MUsh6dO9ls/"}]}
和处理程序...
public void ProcessRequest(HttpContext context) {
HttpResponse httpResponse = context.Response;
httpResponse.ContentType = "application/json";
var json = new JsonWrapper();
var users = from user in _instagramPromotionUsersDataContext.instagramPromotionUsers
select user;
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
foreach (var user in users) {
HttpWebRequest instagramWebRequest = (HttpWebRequest)WebRequest.Create(string.Format(INSTAGRAM_ENDPOINT, user.id.ToString()));
instagramWebRequest.Method = WebRequestMethods.Http.Get;
instagramWebRequest.ContentType = "application/json; charset=utf-8";
try {
using(HttpWebResponse instagramWebResponse = (HttpWebResponse) instagramWebRequest.GetResponse()) {
var dataContractJsonSerializer = new DataContractJsonSerializer(typeof (InstagramObject));
InstagramObject instagramObject = (InstagramObject) dataContractJsonSerializer.ReadObject(instagramWebResponse.GetResponseStream());
json.info.Add(new Dictionary<string, object>() {
{"userName", user.userName},
{"id", user.id.ToString()},
{"name", user.name},
{"data", instagramObject.data}
});
}
} catch (WebException e) {
//do nothing just go to next record if exception is thrown
}
}
httpResponse.Write(javaScriptSerializer.Serialize(json));
}