无效的 JSON 原语:id

发布于 2024-12-10 15:28:55 字数 799 浏览 0 评论 0 原文

我无法让以下功能正常工作。好像序列化错了。这是不同数据变体的第五次迭代。我最初只是做 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());
        }
    });
}

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

I cannot get the following function to work properly. It seems to be serializing it wrong. This is about the 5th iteration of different data variants. I was originally just doing data: {'id': id} like I do at work with WCF, but with the ASMX it just isn't working. It looks like it's serializing teh data as id=1234 instead of id:1234, but I'm fairly new to this. Any help would be appreciated. Oh, and I can call the service directly in the browser and it returns the data properly so I know it's not the service.

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());
        }
    });
}

p.s. I know there are like 10 identical questions but none of them have answers that I could find or that worked for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷乱花海 2024-12-17 15:28:55

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

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

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

据我所知,jQuery 中实际上没有任何 JSON 序列化代码。显然 John Resig 建议使用 Douglas Crockford 的 json2.js

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

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

The simplest possible fix would be to change the line beginning var jsdata to:

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

The problem is that jQuery is encoding jsdata as form data, not as json. The dataType parameter influences how the response is parsed, not how the POST data is encoded.

There's not actually any JSON serialization code in jQuery to the best of my knowledge. Apparently John Resig suggests using Douglas Crockford's json2.js.

To use it, add a script reference to json.js and then:

var jstext = JSON.stringify(jsdata, null, 2);
时光无声 2024-12-17 15:28:55

我现在解决了这个问题。

您需要以这种格式传递 url:

http://domain.com.br/service .asmx/method?objParam={q : "search"}

在你的 service.asmx 文件中,你需要声明这个方法:

Public Function method(objParam As Dictionary(Of String, String)) 

End Function

在你的代码中,看起来像:

function getVentID(id) {
  var jsdata = {
    "id": +id
  }
  var sData = JSON.stringify(jsdata); //convert your json in string
  $.ajax({
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    url: 'services/UserService.asmx/getVentID',
    data: {
      id: sData
    },
    dataType: 'json',
    success: function(msg) {
      alert(msg.d);
    },
    error: function(a, b, c) {
      alert('Error: ' + a.toString() + ' ' + b.toString() + " " + c.toString());
    }
  });
}

i solved this problem right now.

You need pass the url in this format:

http://domain.com.br/service.asmx/method?objParam={q : "search"}

And in your service.asmx file, you need declare this method:

Public Function method(objParam As Dictionary(Of String, String)) 

End Function

In your code, looks like:

function getVentID(id) {
  var jsdata = {
    "id": +id
  }
  var sData = JSON.stringify(jsdata); //convert your json in string
  $.ajax({
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    url: 'services/UserService.asmx/getVentID',
    data: {
      id: sData
    },
    dataType: 'json',
    success: function(msg) {
      alert(msg.d);
    },
    error: function(a, b, c) {
      alert('Error: ' + a.toString() + ' ' + b.toString() + " " + c.toString());
    }
  });
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文