【问题标题】:Why my ajax cannot retrieve ajax code为什么我的 ajax 无法检索 ajax 代码
【发布时间】:2011-03-18 12:01:17
【问题描述】:

我在处理 json 时遇到了一些问题,请帮助我 T.T 我的 ajax

//submit the form
$.ajax({
      type: "POST",
      url: url,
      data: $('#form').serialize(),
      dataType: "json",
      success: function(resp){
          alert(resp);      
          $('#' + thediv).html(resp);
      },
      error:function (xhr, ajaxOptions, thrownError){
           alert(xhr.status);
           alert(thrownError);
      }    
});

它只是警报(200)然后警报(取消定义)

当我将数据类型更改为 html 时,所有数据都可以检索 但是 json 文件是分页打印的, 这是扩展问题吗?

我的 Json 文本

{ 
  "menu": "File", 
  "commands": [ 
      {
          "title": "New", 
          "action":"CreateDoc"
      }, 
      {
          "title": "Open", 
          "action": "OpenDoc"
      }, 
      {
          "title": "Close",
          "action": "CloseDoc"
      }
   ] 
}

【问题讨论】:

  • 服务器发送的 Content-Type 是什么?它应该是应用程序/json。您可以通过 Firebug 或 curl 等方式确定当前值。

标签: jquery ajax json


【解决方案1】:

如果您要连接到 asp.net MVC 或 WCF,也许将此行添加到您的 ajax 调用中

                contentType: "application/json; charset=utf-8",

【讨论】:

  • contentType 是您传递给服务器的内容类型。 serialize返回的数据类型是不是 JSON。
  • 所以我必须将我的帖子数据格式化为 json 代码,然后传入数据。顺便说一句,我忘了提到我正在使用 php 来做 json-ajax 的事情。
  • 但 jquery 网站声明:“dataTypeString 默认值:智能猜测(xml、json、脚本或 html)您期望从服务器返回的数据类型。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-29
  • 2018-11-16
相关资源
最近更新 更多