如何解码JSON.stringify(数据) [英] How to decode JSON.stringify(data)
问题描述
我正在发送List< trip>从我的控制器到我的Ajax的类(id,tripdetails,date),
我无法解码Ajax的Success函数列表,如何获取我的List的所有值< trip>在Ajax成功功能。
我的代码是这样的
控制器:
I am sending List <trip> class(id,tripdetails,date) from my controller to my Ajax,
i am unable to decode the list on Success function of Ajax, How i can get all value of my List<trip> in Ajax success function.
my code is like this
controller:
[HttpGet]
public ActionResult TripLoad()
{
List<trip> lst = repo.TripDetails();
return Json(lst, JsonRequestBehavior.AllowGet);
}
//ajax
$.ajax({
url: '/Trip/TripLoad',
//url: @Url.Action("TripLoad", "Trip"),
dataType: "json",
type: "GET",
contentType: 'application/json; charset=utf-8',
cache: false,
data: {},
success: function (data) { //I want here All List of Trip data or how i can use loop to get data here.
alert("Received " + data)
var d=JSON.stringify(data);
// alert(d.GetAllResult.Name);
},
error: function (xhr) {
alert("Error Message is sanjib " + xhr.responseText);
}
});
推荐答案
.ajax({
url: ' / Trip / TripLoad',
// url:@ Url.Action(TripLoad,Trip),
dataType: json,
type: GET,
contentType:' application / json; charset = utf-8',
cache: false ,
data:{},
成功: function (data){ // 我想在这里所有旅行列表数据或我如何使用循环来获取数据。
alert( 收到 +数据)
var d = JSON 跨度> .stringify(数据);
// alert(d.GetAllResult.Name);
},
错误: function (xhr){
alert( < span class =code-string>错误消息是sanjib + xhr.responseText);
}
});
.ajax({ url: '/Trip/TripLoad', //url: @Url.Action("TripLoad", "Trip"), dataType: "json", type: "GET", contentType: 'application/json; charset=utf-8', cache: false, data: {}, success: function (data) { //I want here All List of Trip data or how i can use loop to get data here. alert("Received " + data) var d=JSON.stringify(data); // alert(d.GetAllResult.Name); }, error: function (xhr) { alert("Error Message is sanjib " + xhr.responseText); } });
做这样的事情,
Do something like this,
success: function(data)
{
for(var i=0; i <data.length; i++) {
console.log(data[i].TripID); // I don't know your model structure, so I assumed one property...
}
}
数据已作为JSON收到,您无需再将其转换为它。
现在,使用 for循环
遍历列表并使用你的model属性来获取值。
如果你有任何查询,我们将进一步讨论。 :)
-KR
Data is already received as a JSON, you don't have to convert it again to it.
Now, iterate through list using for loop
and use your model property to get the value.
If you have any query, we'll discuss it further. :)
-KR
目前还不清楚为什么你会stringify
从服务器收到的数据。即使数据不是JSON,你会用JSON表示它做什么?我宁愿假设服务器为您提供JSON格式,而您需要执行与stringify
相反的操作,这并不奇怪JSON.parse
:
https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse [ ^ ],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON [ ^ ]。
-SA
It's quite unclear why would you everstringify
data received from the server. Even if the data is not JSON, what would you do with JSON representation of it? I would rather assume that the server gives you JSON form, and you need to do the opposite tostringify
, which is, not surprisinglyJSON.parse
:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON[^].
—SA
这篇关于如何解码JSON.stringify(数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!