无效的JSON原语:id [英] Invalid JSON primitive: id

查看:94
本文介绍了无效的JSON原语:id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使以下功能正常工作.似乎将其序列化是错误的.这是关于不同数据变量的第5次迭代.我最初只是在处理数据:{'id':id}就像在使用WCF时所做的那样,但是对于ASMX而言,它却无法正常工作.看起来它正在将数据序列化为id = 1234而不是id:1234,但是我对此并不陌生.任何帮助,将不胜感激.哦,我可以直接在浏览器中调用该服务,它会正确返回数据,因此我知道它不是该服务.

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.我知道大约有10个相同的问题,但没有一个我能找到或对我有用的答案.

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.

推荐答案

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

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

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

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

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.

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

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.

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

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

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

这篇关于无效的JSON原语:id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆