gpt4 book ai didi

web-services - 无效的 JSON 原语 : id

转载 作者:行者123 更新时间:2023-12-03 22:03:02 24 4
gpt4 key购买 nike

我无法让以下功能正常工作。好像序列化错了。这是不同数据变体的第五次迭代。我最初只是做 data: {'id': id} 就像我在使用 WCF 时所做的那样,但使​​用 ASMX 时它不起作用。看起来它正在将数据序列化为 id=1234 而不是 id:1234,但我对此还很陌生。任何帮助,将不胜感激。哦,我可以直接在浏览器中调用该服务,它会正确返回数据,所以我知道这不是该服务。

function getVentID(id) {
//look up id in database and get VentID
alert('id: ' + id);
var jsdata = { "id": + id}
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: 'services/UserService.asmx/getVentID',
data: jsdata,
dataType: 'json',
success: function (msg) {
alert(msg.d);
},
error: function (a, b, c) {
alert('Error: ' + a.toString() + ' ' + b.toString() + " " + c.toString());
}
});
}

附:我知道大约有 10 个相同的问题,但没有一个问题能找到我能找到的或对我有用的答案。

最佳答案

最简单的修复方法是将 var jsdata 开头的行更改为:

var jsdata = '{id:' + id + '}';

问题是 jQuery 将 jsdata 编码为表单数据,而不是 json。 dataType 参数影响响应的解析方式,而不是 POST 数据的编码方式。

据我所知,jQuery 中实际上没有任何 JSON 序列化代码。显然John Resig suggests using Douglas Crockford's json2.js .

要使用它,请添加对 json.js 的脚本引用,然后:

var jstext = JSON.stringify(jsdata, null, 2);

关于web-services - 无效的 JSON 原语 : id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7830961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com