Ajax 请求负载包含意外编码
Ajax Request Payload contains unexpected encoding
我正在尝试通过 Ajax 将变量传递给检索数据以填充 DataTable。我还没有找到原因的解释,但我的 Ajax 有效负载导致了一种奇怪的编码,其中包含 %
和 =
符号以及一些随机字符的组合。我玩过 contentType 和我使用 JSON.stringify() 的方式,但都没有纠正我遇到的意外输出。
Javascript Ajax 请求:
var group = { groupId: 123456789 }
...
"ajax": {
type: 'POST',
url: window.SiteRootURL + 'Group/GetGroup',
contentType: 'application/json',
data: JSON.stringify(group),
"error": function (xhr, error, thrown) {
notfiy({ status: "Error", message: thrown }, 'error');
}
控制器:
public ActionResult GetGroup(Int32 groupId)
{
// do something
}
Ajax 请求负载:
0=%7B&1=%22&2=g&3=r&4=o&5=u&6=p&7=A&8=r&9=t&10=i&11=f&12=a&13=c&14=t&15=I&16=d&17=%22&18=%3A&19=1&20=2&21=3&22=4&23=5&24=6&25=7&26=8&27=9&28=%7D
预期Ajax请求负载:
{groupId: 123456789}
感谢任何帮助:)
此问题与 DataTables. In the Ajax request, ajax.data
does not accept a string. In order to post JSON data you must return the string in a function. The below is an example from the documentation 直接相关:
$('#example').dataTable( {
"ajax": {
"url": "data.json",
"contentType": "application/json",
"type": "POST",
"data": function ( d ) {
return JSON.stringify( d );
}
}
} );
我正在尝试通过 Ajax 将变量传递给检索数据以填充 DataTable。我还没有找到原因的解释,但我的 Ajax 有效负载导致了一种奇怪的编码,其中包含 %
和 =
符号以及一些随机字符的组合。我玩过 contentType 和我使用 JSON.stringify() 的方式,但都没有纠正我遇到的意外输出。
Javascript Ajax 请求:
var group = { groupId: 123456789 }
...
"ajax": {
type: 'POST',
url: window.SiteRootURL + 'Group/GetGroup',
contentType: 'application/json',
data: JSON.stringify(group),
"error": function (xhr, error, thrown) {
notfiy({ status: "Error", message: thrown }, 'error');
}
控制器:
public ActionResult GetGroup(Int32 groupId)
{
// do something
}
Ajax 请求负载:
0=%7B&1=%22&2=g&3=r&4=o&5=u&6=p&7=A&8=r&9=t&10=i&11=f&12=a&13=c&14=t&15=I&16=d&17=%22&18=%3A&19=1&20=2&21=3&22=4&23=5&24=6&25=7&26=8&27=9&28=%7D
预期Ajax请求负载:
{groupId: 123456789}
感谢任何帮助:)
此问题与 DataTables. In the Ajax request, ajax.data
does not accept a string. In order to post JSON data you must return the string in a function. The below is an example from the documentation 直接相关:
$('#example').dataTable( {
"ajax": {
"url": "data.json",
"contentType": "application/json",
"type": "POST",
"data": function ( d ) {
return JSON.stringify( d );
}
}
} );