3

当我通过 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 错误的原因是什么。

4

7 回答 7

3

它可能是由调用的模块引起的mod_security,并且可能导致此问题。你的代码对我来说看起来不错。因此,请检查您的主机,查看是否mod_security已安装并启用,如果是,请尝试暂时禁用它,然后再次测试此代码。如果mod_security不是罪魁祸首,请不要忘记重新启用它。

于 2014-08-25T16:05:16.523 回答
2

问题是您发送的帖子数据为application/x-www-form-urlencoded,而您的服务可能需要application/json。尝试发送实际的 JSON:

gmtjax({
    url: gmt.contextPath + 'tests/questions/process_form',
    type: 'POST',
    data: JSON.stringify(data),
    contentType: "application/json",
    dataType: 'json',
    $spinner: gmt.$spinnerContainer,
    success: function(returnData) {
      console.log('success');
    },
    error: function(){
      //console.log('error');
    },
    $errorContainer: gmt.$mainContainer
});
于 2014-08-25T20:26:24.700 回答
1

尝试

在处理表单页面的 php 结束时

echo print_r($_POST);

success回调

console.log(returnData);

如果请求,响应成功,应该返回

Array
(
    [test_id] => 1
    [action] => update
    [questions] => Array
        (
            [0] => Array
                (
                    [question] => Exploitation strategies seek to create value from unfamiliar resources and activities.
                    [options] => Array
                        (
                            [0] => Array
                                (
                                    [name] => True
                                )

                            [1] => Array
                                (
                                    [name] => False
                                )

                        )

                )

        )

)
1

error回调

error : function(jqxhr, textStatus, errorThrown) {
  console.log(textStatus, jqxhr.getAllResponseHeaders()
             , errorThrown)
}

ajaxSetup

$.ajaxSetup({
   statusCode : {
            200 : function (data, textStatus, jqxhr) {
                    console.log(data);
            },
            406 : function (jqxhr, textStatus, errorThrown) {
                    console.log(textStatus + "\n" + errorThrown);
            }
        }
 });

尝试记录200成功,406状态代码错误。

于 2014-08-27T00:29:35.680 回答
1

406 Not Acceptable error using jQuery AJAX 原来问题是我使用 POST 方法提交了 ajax 请求,但实际上并没有发送任何 post 数据。例如:

  $.ajax({
   type: "POST",
   url: "index.php?action=foo",
   success: function(msg){
 
    }
  });
于 2021-03-24T04:41:33.720 回答
0

您所描述的症状是您的网络主机放置的网络应用程序防火墙的特征。联系他们的支持部门。

于 2014-08-05T03:44:20.027 回答
0

尝试将 AJAX 调用的类型从 POST 类型更改为 GET。它可能会帮助你。

于 2014-08-24T19:00:20.330 回答
0

可能是标题问题。尝试将此添加到您的$.ajax()

headers: { 
    Accept : "application/json"
}
于 2016-03-28T06:16:08.020 回答