Sha*_*ggy 3 c# ajax jquery json
我GenericHandler.ashx.cs
使用jquery ajax请求和json作为数据类型将json数据传递给我的通用处理程序页面.
在我的处理程序代码中,我想以字符串格式返回html表.这是我的处理程序代码的快照
context.Response.ContentType = "text/plain";
context.Response.Write(sResponse);
Run Code Online (Sandbox Code Playgroud)
其中sResponse包含 <table><tr><td>PropertyName</td><td>PropertyID</td></tr><tr><td>abc</td><td>1</td></tr></table>
我的jquery代码(检查错误函数中的内联注释):
id = { 'PropertyID': id };
$.ajax("Handlers/GenericHandler.ashx?Type=getProperties",
{
type: 'post',
dataType: 'json',
cache: false,
contentType: "application/json",
data: JSON.stringify(id),
success: function (data) {
console.log(data);
},
error: function (xhr, status) {
console.log(status); // Output as parseError
console.log(xhr.responseText); // My sResponse string ! But Why Here ?
}
});
Run Code Online (Sandbox Code Playgroud)
我的问题 :
从这里的 jQuery文档中,您可以看到该dataType
参数是AJAX期望作为服务器响应的内容.该contentType
参数是您对服务器的请求中的标头中放置的参数,以便服务器知道如何读取请求.
因此,您应该将您更改dataType
为"文本",如:
$.ajax("Handlers/GenericHandler.ashx?Type=getProperties",
{
type: 'post',
cache: false,
dataType: 'text',
contentType: "application/json",
data: JSON.stringify(id),
success: function (data) {
console.log(data);
},
error: function (xhr, status) {
console.log(status);
console.log(xhr.responseText);
}
});
Run Code Online (Sandbox Code Playgroud)
我发现这在警告数据库上成功的INSERT或UPDATE调用时很有用.为这样的任务创建和返回JSON对象是无关紧要的.
归档时间: |
|
查看次数: |
31781 次 |
最近记录: |