I am receiving the 406 error when I am sending a JSON data object through the jQuery AJAX function to a backend service so the data can be stored into the database.
AJAX FUNCTION
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 structure:
{
"test_id": "1",
"action": "update",
"questions": [
{
"question": "Exploitation strategies seek to create value from unfamiliar resources and activities.",
"options": [
{
"name": "True"
},
{
"name": "False"
}
]
}
]
}
Process Form Function (Backend)
function process_form(){
print_r($_POST);
}
When I submit the data the STATUS CODE on the XHR request is 406 Not Acceptable.
Request Header
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
RESPONSE
false
When the request fails it does not even enter the process_form function to print out the POST array.
However, when I modify the 'create value' string in the question to something like 'create a value' the form submits successfully. The only thing I can think of is some SQL Injection prevention detection on the server layer (GoDaddy) but I am unsure on how to address this.
What could be causing the 406 error when Content-Type is obviously not the issue.
It could be caused by a module called mod_security
and it can cause this problem. Your code looks fine to me. So, check out your host, see if mod_security
is installed and enabled, and if it is, try disabling it temporarily and then test this code again. If mod_security
is not the culprit, don't forget to re-enable it.