【发布时间】:2011-08-07 03:31:46
【问题描述】:
我花了六个小时试图弄清楚这一点,但我没有成功。
我的本地计算机上有一个 HelloWorld .NET 3.5 Web 服务。 Set up as required.
该服务返回自定义结构的List。
我正在尝试使用 jQuery 1.4.4 来使用它。
当我尝试执行the documentation 所说的操作时,我总是从服务返回一个 XML 响应,这会导致 jQuery 中的 parseerror 或作为哑字符串传递给 success 函数。 也就是说,但是我结合了 dataType 和 accepts(根据文档,控制如何处理接收到的数据),我得到了一个 XML。
但是,当我做一些不符合文档逻辑的事情时,我成功地获得了我的对象数组。 也就是说,当我忽略 dataType 和 accepts 并设置 contentType: "application/json; charset=utf-8" 时,它可以正常工作。 但是 contentType,根据文档,控制正在发送的数据 到服务器,没有收到。
在代码中:
$.ajax(
{
type: "GET",
url: "http://localhost:52624/Service1.asmx/HelloWorld",
dataType: "json",
//accepts can be anything, or it can be missing, doesn't matter, only depends on dataType
success: function(data, textStatus, jqXHR) {...},
error: function(jqXHR, textStatus, errorThrown) {...}
}
)
结果:调用错误处理程序,textStatus = parseerror。
$.ajax(
{
type: "GET",
url: "http://localhost:52624/Service1.asmx/HelloWorld",
dataType: "application/json",
//accepts can be anything, or it can be missing, doesn't matter, only depends on dataType
success: function(data, textStatus, jqXHR) {...},
error: function(jqXHR, textStatus, errorThrown) {...}
}
)
结果:Web 服务返回 XML,它作为 string 传递给成功处理程序。
$.ajax(
{
type: "GET",
url: "http://localhost:52624/Service1.asmx/HelloWorld",
accepts: "json", // or "application/json"
success: function(data, textStatus, jqXHR) {...},
error: function(jqXHR, textStatus, errorThrown) {...}
}
)
结果:Web 服务返回 XML,它被解析并作为 IXMLDOMDocument2 传递。
$.ajax(
{
type: "GET",
url: "http://localhost:52624/Service1.asmx/HelloWorld",
contentType: "application/json; charset=utf-8",
success: function(data, textStatus, jqXHR) {...},
error: function(jqXHR, textStatus, errorThrown) {...}
}
)
结果:Web 服务返回 JSON,它被 jQuery 部分解析(数字和字符串被解析为对象的属性,但日期仍为 "/Date(1303003305724)/" 的形式)。
问题:
- 我了解 jQuery 规范吗?为什么说控制发送数据的参数实际上控制接收数据?
- 我做错了什么?
- 获取由 jQuery 解析的日期的最后一步是什么?
【问题讨论】:
-
@StephenKennedy 这个问题在两年后出现,可以说包含的材料要少得多。如果有的话,您应该投票关闭 it 作为重复项。
-
我认为较新的版本更直接,对普通读者更有用,但我接受这是一个判断电话,其他人可能不同意:)
-
我撤回了我的投票,因为这个更关注 jQuery 而我今天正在看 JavaScriptSerializer 的东西。
标签: web-services .net-3.5 jquery