【问题标题】:jQuery: return string response in jquery ajax json calljQuery:在 jquery ajax json 调用中返回字符串响应
【发布时间】: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 ?
        }
    });

我的问题:

  1. 为什么我在成功函数中没有得到响应
  2. 这是正确的做法吗?或者我应该将 html 表转换为 json 对象然后返回它。并再次以表格格式显示?

【问题讨论】:

  • 响应不是JSON,那你为什么设置dataType: 'json'?
  • 请求数据在JSON这就是为什么!
  • 将dataType: 'json', 更改为dataType: 'text/plain',
  • @Satpal m 如果我将 dataType 更改为 text/plain,则会出现同样的错误
  • 各位,我有一个关于这个话题的问题。 $.ajax("Handlers/GenericHandler.ashx?Type=getProperties", 不是无效的吗?我们可以像这样使用url 属性吗?

标签: c# jquery json ajax


【解决方案1】:

从 jQuery 文档 here,您可以看到 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); 
    }
});

我发现这在提醒数据库上的成功 INSERT 或 UPDATE 调用时很有用。为此类任务创建和返回 JSON 对象是多余的。

【讨论】:

    【解决方案2】:

    您的响应不是有效的 JSON,因为它返回的是纯文本。 jQuery 期望响应为 JSON,因为您已设置 contentType: "application/json"

    如果您网站的其余部分使用 JSON 作为传输格式,则将您的 HTML 包装为 JSON 对象并返回它。

    在您的后端代码中,返回如下所示的内容

    {response_html : "<table><tr><td>PropertyName</td><td>PropertyID</td></tr><tr><td>abc</td><td>1</td></tr></table>"}
    

    并且在您的 jQUery 代码中,您可以在成功回调中访问它。

    success: function (data) {
        console.log(data.response_html);            
    },
    

    注意 - 您需要从后端代码中删除纯文本内容类型并生成 JSON。

    【讨论】:

      【解决方案3】:

      如果您告诉 $.ajax 您需要 JSON,则来自服务器的 text/plain 响应不是有效响应。

      关于您的第二个问题:最好的方法是以 JSON 格式返回您要使用的数据,例如:

      [
        { "label" : "PropertyName", "value" : "abc" },
        { "label" : "PropertyId", "value" : "1" }
      ]
      

      然后在您的 Ajax 请求的成功回调中,使用该数据使用 jQuery 构建您的 HTML 结构。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-24
        • 2011-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-12
        • 1970-01-01
        相关资源
        最近更新 更多