I'm using jQuery to call an asmx and return some data. I'm making the call like this
我正在使用jQuery调用asmx并返回一些数据。我这样打电话
function getRequestInfo(event) {
var id = $('#<%= RequestDaysId.ClientID %>').val();
var formattedId = "{'id': '115'}";
$.ajax({
type: "Post",
url: "services/VacationServices.asmx/GetVacationInfo",
data: "{'id': '" + id + "'}",
dataType: "json",
contentType: "application/json; charset=utf-8",
processdata: true,
success: function(data) {
$('#<%=Note.ClientID %>').val(data.Note);
$('.pendingrequestinfo').show().fadeIn(2000);
},
error: function(result, errortype, exceptionobject) {
$('.failureMessage').fadeIn(2000).fadeOut(2000);
}
})
};
Everything seems to be working fine, I set a break point in my success function and inspect the data object and see this.
一切似乎工作正常,我在我的成功函数中设置了一个断点并检查数据对象并看到这一点。
"{"Note":"this is a note","dayInfo":[{"ShortDate":"3/4/2010","DayType":"Vacation","HalfDay":""},{"ShortDate":"3/5/2010","DayType":"Vacation","HalfDay":""}]}"
The problem comes when I try to get the values out of the JSON. If I do something like data.Note, I get undefined back.
当我尝试从JSON中获取值时,问题就出现了。如果我做像data.Note这样的事情,我会得到未定义的回复。
It's late, It's Saturday and I've been at it all day, I sure would like a push in the right direction when it comes to parsing though my JSON.
已经很晚了,星期六,我整天都在这里,我确实希望通过解析我的JSON来推动正确的方向。
EDIT: I'm using Asp.net and JavaScriptSerializer.Serialize() to create the JSON. When I set a break point and inspect the 'data' object it looks to have a property d that contains the string that should be JSON.
编辑:我正在使用Asp.net和JavaScriptSerializer.Serialize()来创建JSON。当我设置一个断点并检查'data'对象时,它看起来有一个包含应该是JSON的字符串的属性d。
ANOTHER EDIT: If I do something like this in my success
另一个编辑:如果我在成功的过程中做了类似的事情
$('#<%=Note.ClientID %>').val(data.d.[0]);
I get the { opening curly brace. I guess i'm getting a string instead of JSON, but it seems to go against what the jquery api states about the return value when the datatype is set to JSON.
我得到{开口大括号。我想我得到的是一个字符串而不是JSON,但是当数据类型设置为JSON时,它似乎违背了jquery api关于返回值的内容。
Thanks guys. Jim
多谢你们。吉姆
4 个解决方案
#1
2
First make sure that the JSON string exists in the "d" variable in the response returned in the success callback. Next, in order to get the JSON object you will need to convert the string into the JSON. You can use the eval function or the JQuery built in function to convert string to JSON. I like the jquery-json plug in to convert string into JSON representation.
首先确保JSON字符串存在于成功回调中返回的响应中的“d”变量中。接下来,为了获取JSON对象,您需要将字符串转换为JSON。您可以使用eval函数或JQuery内置函数将字符串转换为JSON。我喜欢jquery-json插件将字符串转换为JSON表示。
Your code will look something like this:
您的代码将如下所示:
var jsonObject = eval('(' + data.d + ')');
Now, you can use jsonObject.Note or any other property.
现在,您可以使用jsonObject.Note或任何其他属性。
With jquery-json plugin you can do the following:
使用jquery-json插件,您可以执行以下操作:
var note = $.evalJSON(data.d).Note;
#2
1
That's not a valid JSON format. Remove the doublequotes at beginning and end to make it fullworthy JSON.
这不是有效的JSON格式。删除开头和结尾的双引号,使其成为完整的JSON。
#3
1
This one is so silly...sorry guys. When returning data from the asmx there is no need to serialize it into JSON
这个太傻了......对不起伙计们。从asmx返回数据时,无需将其序列化为JSON
I have the following class that I'm populating and returing from my web method
我有以下课程,我正在填充和退出我的网络方法
public class VacationInfo
{
public string Note { get; set; }
public List<DayInfo> dayInfo { get; set; }
public VacationInfo(string note, List<DayInfo> dayinfo)
{
this.Note = note;
this.dayInfo = dayinfo;
}
public class DayInfo
{
public string ShortDate { get; set; }
public string DayType { get; set; }
public string HalfDay { get; set; }
public DayInfo(string shortdate, string daytype, string halfday)
{
this.ShortDate = shortdate;
this.DayType = daytype;
this.HalfDay = halfday;
}
}
}
as long as your web service is decorated with
只要您的Web服务被装饰
[System.Web.Script.Services.ScriptService]
your object will be serialized and returned as JSON at no charge to you. :)
您的对象将被序列化并作为JSON免费返回给您。 :)
then I'm able to do this
然后我就能做到这一点
data.d.Note
in my call back.
在我的回电话中。
Thanks for the help guys.
谢谢你的帮助。
信用到期的信用
#4
1
Here is how I process. Note the dataFilter: part - this makes it work with either newer or older asp.net stuff.
这是我的处理方式。注意dataFilter:part - 这使得它可以使用更新或更旧的asp.net东西。
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: objectSaveData,
dataFilter: function(data)
{
var msg;
if (typeof (JSON) !== 'undefined' &&
typeof (JSON.parse) === 'function')
msg = JSON.parse(data);
else
msg = eval('(' + data + ')');
if (msg.hasOwnProperty('d'))
return msg.d;
else
return msg;
},
url: "/mywebservice.asmx/myMethodName",
success: function(msg)
{
//do stuff
},
failure: function(msg)
{
//handlefail
}
});
#1
2
First make sure that the JSON string exists in the "d" variable in the response returned in the success callback. Next, in order to get the JSON object you will need to convert the string into the JSON. You can use the eval function or the JQuery built in function to convert string to JSON. I like the jquery-json plug in to convert string into JSON representation.
首先确保JSON字符串存在于成功回调中返回的响应中的“d”变量中。接下来,为了获取JSON对象,您需要将字符串转换为JSON。您可以使用eval函数或JQuery内置函数将字符串转换为JSON。我喜欢jquery-json插件将字符串转换为JSON表示。
Your code will look something like this:
您的代码将如下所示:
var jsonObject = eval('(' + data.d + ')');
Now, you can use jsonObject.Note or any other property.
现在,您可以使用jsonObject.Note或任何其他属性。
With jquery-json plugin you can do the following:
使用jquery-json插件,您可以执行以下操作:
var note = $.evalJSON(data.d).Note;
#2
1
That's not a valid JSON format. Remove the doublequotes at beginning and end to make it fullworthy JSON.
这不是有效的JSON格式。删除开头和结尾的双引号,使其成为完整的JSON。
#3
1
This one is so silly...sorry guys. When returning data from the asmx there is no need to serialize it into JSON
这个太傻了......对不起伙计们。从asmx返回数据时,无需将其序列化为JSON
I have the following class that I'm populating and returing from my web method
我有以下课程,我正在填充和退出我的网络方法
public class VacationInfo
{
public string Note { get; set; }
public List<DayInfo> dayInfo { get; set; }
public VacationInfo(string note, List<DayInfo> dayinfo)
{
this.Note = note;
this.dayInfo = dayinfo;
}
public class DayInfo
{
public string ShortDate { get; set; }
public string DayType { get; set; }
public string HalfDay { get; set; }
public DayInfo(string shortdate, string daytype, string halfday)
{
this.ShortDate = shortdate;
this.DayType = daytype;
this.HalfDay = halfday;
}
}
}
as long as your web service is decorated with
只要您的Web服务被装饰
[System.Web.Script.Services.ScriptService]
your object will be serialized and returned as JSON at no charge to you. :)
您的对象将被序列化并作为JSON免费返回给您。 :)
then I'm able to do this
然后我就能做到这一点
data.d.Note
in my call back.
在我的回电话中。
Thanks for the help guys.
谢谢你的帮助。
信用到期的信用
#4
1
Here is how I process. Note the dataFilter: part - this makes it work with either newer or older asp.net stuff.
这是我的处理方式。注意dataFilter:part - 这使得它可以使用更新或更旧的asp.net东西。
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: objectSaveData,
dataFilter: function(data)
{
var msg;
if (typeof (JSON) !== 'undefined' &&
typeof (JSON.parse) === 'function')
msg = JSON.parse(data);
else
msg = eval('(' + data + ')');
if (msg.hasOwnProperty('d'))
return msg.d;
else
return msg;
},
url: "/mywebservice.asmx/myMethodName",
success: function(msg)
{
//do stuff
},
failure: function(msg)
{
//handlefail
}
});