jQuery 无效的 JSON 原语:id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7830961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:18:52  来源:igfitidea点击:

Invalid JSON primitive: id

web-servicesjqueryasmx

提问by Devil's Advocate

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.

我无法使以下功能正常工作。它似乎是错误的序列化。这是关于不同数据变体的第 5 次迭代。我最初只是在做 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());
        }
    });
}

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.

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

回答by sblom

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

最简单的解决方法是将行开始更改var jsdata为:

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

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

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

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.

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

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

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

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

回答by Gustavo Carvalho

i solved this problem right now.

我现在解决了这个问题。

You need pass the url in this format:

您需要以这种格式传递网址:

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

http://domain.com.br/service.asmx/method?objParam={q:“搜索”}

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

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

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