jQuery 无效的 JSON 原语 ERROR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17873926/
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-26 20:07:45  来源:igfitidea点击:

Invalid JSON primitive ERROR

jqueryajax

提问by Nithin Paul

Please Help. In my ajax call getting error Invalid JSON primitive, whats wrong with this following ajax call

请帮忙。在我的 ajax 调用中得到错误 Invalid JSON 原语,下面的 ajax 调用有什么问题

    $.ajax({
                url: "/Precedent/ShowPartyContents", type: "POST",
                contentType: 'application/json; charset=utf-8',
                dataType: 'html',
                data:{'partyId':party,'PartySelCombo':valueFrom,'DocumentId':DocId},
                sucess:function(result){
                    alert("String"+ result);
                    //jq("#PartyTagContentArea-"+ pass cheyyenda id).html(data).fadeIn();
                },
                error : function( ts ){ 
                    alert("error :(" + ts.responseText);


                }

            });

Thanks

谢谢

回答by UltraInstinct

You are promising a content type of application/jsonbut are sending a plain JS Object, which gets serialised as percentile-encoded-string by jQuery. This serialization might be far from valid JSON.

您承诺的内容类型为 ,application/json但正在发送一个普通的 JS 对象,该对象被 jQuery 序列化为百分位编码字符串。这种序列化可能与有效的 JSON 相去甚远。

Change:

改变:

data: {'partyId':party,'PartySelCombo':valueFrom,'DocumentId':DocId},

to:

到:

data: JSON.stringify({'partyId':party,'PartySelCombo':valueFrom,'DocumentId':DocId}),

回答by Akki619

Try with, remove " ' " from data,

尝试从数据中删除“'”,

data:{partyId:party,PartySelCombo:valueFrom,DocumentId:DocId}

Use single quote to assign your values like

使用单引号来分配您的值,例如

Wrong:

错误的:

$.ajax({
  type: 'POST',
  contentType: 'application/json',
  dataType: 'json',
  url: 'WebService.asmx/Hello',
  data: { FirstName: "Dave", LastName: "Ward" }
});

Right:

对:

$.ajax({
  type: 'POST',
  contentType: 'application/json',
  dataType: 'json',
  url: 'WebService.asmx/Hello',
  data: '{ FirstName: "Dave", LastName: "Ward" }'
});

Please follow below link for clarifications

请按照以下链接进行澄清

Invalid Json Premitive Possible Reason

无效的 Json Premitive 可能的原因

回答by Code L?ver

You are facing the problem due to these lines:

由于这些行,您正面临问题:

contentType: 'application/json; charset=utf-8',
dataType: 'html',

first you are saying to application that the return result will be JSON type and in second line you say that the dataType will be HTML. Then how can it be return the json data.

首先,您对应用程序说返回结果将是 JSON 类型,在第二行中,您说 dataType 将是 HTML。那么它怎么能返回json数据。

To return and use the json data, you must specify the dataType:'json'. Use this:

要返回和使用 json 数据,您必须指定dataType:'json'. 用这个:

contentType: 'application/json; charset=utf-8',
dataType: 'json',

Note: you have misspelled the success function so correct that also.

注意:您拼错了成功函数,所以也正确。