406-Invalid Answer - jQuery AJAX
I am getting 406 error when I post a JSON data object via jQuery AJAX function to a backend service so that the data can be stored in 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 send data, the STATUS CODE on request XHR 406 is not valid.
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
REACTION
false
When the request fails, it doesn't even enter the process_form function to print the POST array.
However, when I change the "create value" line in the question to something like "create a value", the form is submitted successfully. The only thing I can think of is server-level SQL Injection prevention detection (GoDaddy), but I'm not sure how to solve this.
What could be causing the 406 error when Content-Type is obviously not the problem.
source to share
It can be caused by a named module mod_security
and it can cause this problem. Your code looks good to me. So check your host, see if it is installed and installed mod_security
, and if so, try to temporarily disable it and then check this code again. If mod_security
not the culprit, be sure to turn it back on.
source to share
The problem is that you are sending post data as application/x-www-form-urlencoded
, and your service probably requires application/json
. Try sending the actual 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
});
source to share
Try
when closing php on process form page
echo print_r($_POST);
on success
callback
console.log(returnData);
If the request, a successful response, should return
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
on error
callback
error : function(jqxhr, textStatus, errorThrown) {
console.log(textStatus, jqxhr.getAllResponseHeaders()
, errorThrown)
}
at ajaxSetup
$.ajaxSetup({
statusCode : {
200 : function (data, textStatus, jqxhr) {
console.log(data);
},
406 : function (jqxhr, textStatus, errorThrown) {
console.log(textStatus + "\n" + errorThrown);
}
}
});
try logging success 200
, 406
statusCode error.
source to share