AJax JASON Jquery返回[Object Object] [英] AJax JASON Jquery returning [Object Object]
问题描述
我在我的应用程序中使用ajax jason .net 2.0。当我迁移到.net 4.0时,ajax函数返回一个jason对象作为[Object Object]
$ .ajax({
类型: POST,
async : false ,
超时: 500 ,
url: ../ PageMethod.aspx / Sample,
data:' {Id:' + Id + ' }',
contentType: application / json,
dataType: json,
s uccess:function(result)
{
alert( Before Result +结果);
document.getElementById(panelName).innerHTML = result;
alert( 结果 +结果);
}
});
[WebMethod] public static string 示例( int Id)
{ string msg; StringBuilder sb = new StringBuilder(); sb.Append( < table cellspacing = \3 \cellpadding = \3px \ >中跨度>); // Morecode sb.Append(< / table>); return sb.ToString();
}
任何人都可以告诉我我在哪里做错了。
谢谢
Prashant
注意:当我硬编码document.getElementById(panelName).innerHTML =''HHHH'';其工作正常
< blockquote> .ajax({
type: POST,
async : false ,
timeout: 500 ,
url: ../ PageMethod.aspx / Sample,
data:' {Id:' + Id + ' }',
contentType: application / json,
dataType: json,
成功:函数(结果)
{
alert( 在结果之前 +结果);
document.getElementById(panelName).innerHTML = result;
alert( 结果 +结果);
}
});
[WebMethod] public static string 示例( int Id)
{ string msg; StringBuilder sb = new StringBuilder(); sb.Append( < table cellspacing = \3 \cellpadding = \3px \ >中跨度>); // Morecode sb.Append(< / table>); return sb.ToString();
}
任何人都可以告诉我我在哪里做错了。
谢谢
Prashant
注意:当我硬编码document.getElementById(panelName).innerHTML =''HHHH'';它工作正常
在你的代码中
alert( 在结果之前 +结果); // 使用result.d
alert( 在结果之前 + result.d) // 使用此行
// 每个地方都相同
document.getElementById(panelName).innerHTML = result.d;
alert( 结果 + result.d);
希望这将有助于
从方法返回的是一个html字符串而不是一个json字符串,它是预期的ajax调用(contentType: application / json, dataType: json,)
所以你有两个选择1)改变样本方法作为json字符串返回或2)删除contentType和
dataType来自你的ajax方法 - 默认情况下jquery推断出正确的类型,在本例中为html。
希望这可以解决你的问题。
Hi ,
I am using ajax jason in my application developed in .net 2.0.When i migrated to .net 4.0 the ajax function returning a jason object as [Object Object]
$.ajax({
type: "POST",
async: false,
timeout: 500,
url: "../PageMethod.aspx/Sample",
data: '{"Id":"'+Id+'"}',
contentType: "application/json",
dataType: "json",
success: function(result)
{
alert(" Before Result "+result);
document.getElementById(panelName).innerHTML = result;
alert("Result After "+result);
}
});
[WebMethod] public static string Sample(int Id)
{ string msg; StringBuilder sb = new StringBuilder(); sb.Append("<table cellspacing=\"3\" cellpadding=\"3px\">"); //Morecode sb.Append("</table>"); return sb.ToString();
}
Can any one tell me where I am doing mistake.
Thanks
Prashant
Note: When I hard code document.getElementById(panelName).innerHTML = ''HHHH'';its working fine
.ajax({ type: "POST", async: false, timeout: 500, url: "../PageMethod.aspx/Sample", data: '{"Id":"'+Id+'"}', contentType: "application/json", dataType: "json", success: function(result) { alert(" Before Result "+result); document.getElementById(panelName).innerHTML = result; alert("Result After "+result); } }); [WebMethod] public static string Sample(int Id) { string msg; StringBuilder sb = new StringBuilder(); sb.Append("<table cellspacing=\"3\" cellpadding=\"3px\">"); //Morecode sb.Append("</table>"); return sb.ToString(); }
Can any one tell me where I am doing mistake.
Thanks
Prashant
Note: When I hard code document.getElementById(panelName).innerHTML = ''HHHH'';its working fine
In your code
alert(" Before Result "+result); // use result.d alert(" Before Result "+result.d)// use this line // same every where document.getElementById(panelName).innerHTML = result.d; alert("Result After "+result.d);
Hope this will help up
what you are returning from the method is a html string not a json string which is expected by the ajax call (contentType: "application/json",dataType: "json",)
So you have two choices 1) change the sample method to return as a json string or 2) remove thecontentTypeand
dataTypefrom your ajax method - jquery by default infers the correct type, in this case html.
hope this solves your problem.
这篇关于AJax JASON Jquery返回[Object Object]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!