当我通过 jQuery AJAX 函数将 JSON 数据对象发送到后端服务以便可以将数据存储到数据库中时,我收到 406 错误。
AJAX 函数
data = {
questions: questions,
test_id: test_id,
action: 'update'
};
gmtjax({
url: gmt.contextPath + 'tests/questions/process_form',
type: 'POST',
data: data,
dataType: 'json',
$spinner: gmt.$spinnerContainer,
success: function(returnData) {
console.log('success');
},
error: function(){
//console.log('error');
},
$errorContainer: gmt.$mainContainer
});
JSON结构:
{
"test_id": "1",
"action": "update",
"questions": [
{
"question": "Exploitation strategies seek to create value from unfamiliar resources and activities.",
"options": [
{
"name": "True"
},
{
"name": "False"
}
]
}
]
}
处理表单功能(后端)
function process_form(){
print_r($_POST);
}
当我提交数据时,XHR 请求上的状态码是 406 Not Acceptable。
请求标头
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,af;q=0.6,ms;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Content-Length:1726
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:ci_session=08c62699d06dfcf8ba853cacb350ab3b
Host:testingsite.com
Origin:https://testingsite.com
Pragma:no-cache
Referer:https://testingsite.com/tests/manage/id/194/goto/2
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
X-Requested-With:XMLHttpRequest
回复
false
当请求失败时,它甚至没有进入 process_form 函数来打印出 POST 数组。
但是,当我将问题中的“创建值”字符串修改为“创建值”之类的内容时,表单会成功提交。我唯一能想到的是服务器层(GoDaddy)上的一些 SQL 注入预防检测,但我不确定如何解决这个问题。
当 Content-Type 显然不是问题时,可能导致 406 错误的原因是什么。