【发布时间】:2014-06-25 07:34:27
【问题描述】:
我使用 jquery ajax 请求和 json 作为数据类型将 json 数据传递到我的通用处理程序页面 GenericHandler.ashx.cs。
在我的处理程序代码中,我想以字符串格式返回 html 表。这是我的处理程序代码的快照
context.Response.ContentType = "text/plain";
context.Response.Write(sResponse);
其中 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 ?
}
});
我的问题:
- 为什么我在成功函数中没有得到响应
- 这是正确的做法吗?或者我应该将 html 表转换为 json 对象然后返回它。并再次以表格格式显示?
【问题讨论】:
-
响应不是JSON,那你为什么设置
dataType: 'json'? -
请求数据在
JSON这就是为什么! -
将
dataType: 'json',更改为dataType: 'text/plain', -
@Satpal m 如果我将 dataType 更改为 text/plain,则会出现同样的错误
-
各位,我有一个关于这个话题的问题。
$.ajax("Handlers/GenericHandler.ashx?Type=getProperties",不是无效的吗?我们可以像这样使用url属性吗?