Jquery DataTable将参数传递给ajax调用asp.net。无效的JSON原语 [英] Jquery DataTable passing a parameter to ajax call asp.net. Invalid JSON primitive

查看:441
本文介绍了Jquery DataTable将参数传递给ajax调用asp.net。无效的JSON原语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的java脚本代码,使用服务器数据填充我的HTML表,我正在使用Jquery DataTables来服务于此目的。

Below is my java script code to Populate my HTML table with server data and I am using Jquery DataTables to server this purpose.

function LoadData(result) {
$('#example').DataTable({
"ajax": {
   "dataType": 'json',
   "contentType": "application/json; charset=utf-8",
   "type": "POST",
   "url": "index.aspx/Risky",
   "data": function (d) {
    return JSON.stringify( d )
    //return JSON.stringify(result);
    // d.extra_search = result;      
    //"extra_search": result

   },
   "dataSrc": function (json) {
       return $.parseJSON(json.d);
   }
},
"columns": [
    { "data": "Prctice_Group_Risk_No" },
    { "data": "Practice_Group" },
    { "data": "Risk_Category" },
 ]
});
}

以下是我的代码背后的web方法描述

Below is my code behind web method description

[WebMethod]
[ScriptMethod]
public static string Risky()
{
  return JsonConvert.SerializeObject(riskList);
}

直到现在,它的工作正常,我的web方法被调用,我的HTML表获取

Until now its working fine, My web method gets called and my HTML table gets populated.

但是我的问题是我想将变量result作为参数传递给这个ajax调用,以便我的web方法接收它并返回一个特定的数据基于此参数。

But my problem is that I want to pass the variable "result" as parameter to this ajax call so that my web method receives it and returns me a specific data based over this parameter.

我已访问过 https://datatables.net/reference/option/ajax.data ,并尝试遵循所有在此描述的方法传递额外的数据与我的ajax调用,您可以看到我的java脚本代码三个注释行的代码,我尝试了这三种不同的方法,但没有一个它对我造成一个同样的问题,无效的JSON原语在我的firebug调试器中有一个500服务器状态代码。我可以在我的firebug调试器中看到传递给该方法的参数是extra_search = 123

I have visited https://datatables.net/reference/option/ajax.data and tried to follow all of the methods described there to pass the extra data with my ajax call as you can see in my java script code three commented lines of code,I have tried this three different ways but none of it works for me causing me to a single same problem, "Invalid JSON primitive" with a 500 server status code in my firebug debugger. I can see in my firebug debugger that the parameter being passed to the method is "extra_search=123"

我可以从错误描述中猜出,我添加这个额外的方式参数是不正确的,例如某种程度上它不会使一个正确的json格式。但我不知道如何纠正它。任何人都有帮助。

I can guess from the error description that my way of adding this extra parameter is not correct , e.g somehow it doesn't make a correct json format. but I don't know how to correct it.

推荐答案

借助@Sanjay Kumar NS和此链接

With the help of @Sanjay Kumar N S and this link

https://datatables.net/forums/discussion/ 24546 / ajax-data-invalid-json-primitive-error

我可以解决我的问题。问题是没有有效的格式的JSON数据被发送到服务器,所以服务器抛出无效的JSON原始的例外

I could be able to solve my problem. The problem was that not a valid formatted JSON data was being sent to the server so the server was throwing an exception of "Invalid JSON Primitive"

以下是正确的格式发送一个包含DataTable函数中的额外数据的ajax调用

Following is the correct format to send a an ajax call containing extra data from within the DataTable function

function LoadData(result) {
$('#example').DataTable({
"ajax": {
"dataType": 'json',
"contentType": "application/json; charset=utf-8",
"type": "POST",
"url": "index.aspx/Risky",
"data": function (d) {
 return "{FileName:" + result+ "}";

},
"dataSrc": function (json) {
   return $.parseJSON(json.d);
}
},
"columns": [
{ "data": "Prctice_Group_Risk_No" },
{ "data": "Practice_Group" },
{ "data": "Risk_Category" },
]
});
}

这篇关于Jquery DataTable将参数传递给ajax调用asp.net。无效的JSON原语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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