[英]Jquery ajax to retrieve data from get request returns empty object
I have a Jquery ajax call to a Web API that returns a JSON serialized object. 我有一个对Web API的Jquery ajax调用,该API返回JSON序列化的对象。 This call gets the JSon object properly but how do I parse it to show the city?
此调用正确获取了JSon对象,但是如何解析它以显示城市呢?
$.ajax({
url: '@Url.Action("GetDetails","home")',
data: {'orderId':OId,},
success: function (d,status,xhr) {
alert(d[0].City);
},
error: function (data) {
alert("Error");
}
});
The data returned is this: 返回的数据是这样的:
[{"id":52,
"FName":"John",
"LName":"Smith",
"Street":"Main Street",
"City":"Magic Kingdom",
"State":"FL"}]
The alert in the ajax call returns an "undefined". ajax调用中的警报返回“未定义”。 I am doing basically the same thing with data returned from a Ajax post call and in that case the alert works as expected.
从Ajax调用返回的数据,我基本上都在做同样的事情,在这种情况下,警报按预期工作。 What am I missing?
我想念什么? How do I access the components of the JSON that is returned?
如何访问返回的JSON的组件?
I am watching the data come back from the get successfully (using Fiddler). 我正在看着数据成功地返回(使用Fiddler)。 The returned data looks identical whether it is returned from a post or get.
无论是从帖子还是从get返回的,返回的数据看起来都是相同的。
Try this: 尝试这个:
Here dataType: "json"
tells jQuery that you want it to parse the returned JSON. 在这里
dataType: "json"
告诉jQuery您希望它解析返回的JSON。
$.ajax({
url: '@Url.Action("GetDetails","home")',
data: {
'orderId': OId,
},
dataType: "json",
success: function (d, status, xhr) {
alert(d[0].City);
},
error: function (data) {
alert("Error");
}
});
Try with parseJSON 尝试使用parseJSON
var data= jQuery.parseJSON(d);
alert( data.City);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.