I've just moved a site to live and am getting a 406 Not Acceptable error when using jquery to make an ajax request to a php script. I don't get the error on my test server so I'm just trying to figure out the correct way to fix this. The AJAX request is as follows, and expects JSON response, while my php script is just echoing out with json_encode().
$.ajax({
type: "POST",
contentType: "application/json",
data: "{}",
url: "server/php/progress.php",
dataType: "json",
EDIT: here's the crux of my php script (have also tried without the header setting):
header('Content-type: application/json');
echo json_encode($val);
exit;
Any thoughts on what should be done to fix this? htaccess directives?
Not acceptable will be triggered based on Accept, Accept-Charset, Accept-Language headers. So if your code is identical, it's likely that there's some setting related to content negotiation turned on there.
Aargh. Just switching it to GET sorted the issue. I'm not sure why on earth I was using POST for retrieval.
Still not sure why the issue didn't happen on my local wamp server, some difference in the apache settings somewhere.
Related
The following function should serialize form data and send it to be processed on the server.
function postIt() {
var postData = $("#myForm").serialize();
$.ajax({
type: 'POST',
data: postData,
url: 'php/makeTopics.php',
success: function(data) {
/* do stuff*/
}
})
}
However, I have just started receiving 403 Forbidden errors from the server. Upon investigation, I have found that the .serialize() function replaces whitespace with "+" and that if I resend the data without the "+"s I no longer get the error.
What am I doing wrong? Is this a client or a server issue?
More info:
-Using LITESPEED server,
-I have reduced my php code to <?php echo("Hello World!"); ?> and the problem persists as described, so I think it must be something else in the webserver. Also, this is new behaviour - I have made no code changes at either end to trigger it.
-WORKING DATA EXAMPLE: tn=factorystore&tkw1=manufacturers&tkw2=brickandmortar
-NOT WORKING DATA EXAMPLE: tn=factory+store&tkw1=manufacturers&tkw2=brick+and+mortar
(Note: the above data examples are the 'source' Form Data taken from the Chrome console)
when you say,
reduced my php code to and the problem persists
do you mean, https://yourdomain.com/hello.php return 403?
Generally, You can ask your host to check the server error log to find out why 403 error. 403 could be caused by many reasons. Such as mod_security, or some restriction on some URLs, folders, etc.
I want to get some JSON data from an URL. I did it successfully with PHP and Guzzle, but my site needs some loading time that's why I want to try the data with AJAX. When I try to get the data with an AJAX-Call, this will happen:
CORS-Header 'Access-Control-Allow-Origin' missing
I googled it and tried things out like adding this header in htaccess and htttp.conf and so on. Nothing changed, always getting this error message.
When I tried to retrieve the data as JSONP, I get the message:
Missing ; before Statement
Googled it too, my question is, is there maybe a way to convert JSON to JSONP while I am getting it via AJAX? Or did someone has another idea to solve the cross origin problem?
EDIT:
Data I want to get: https://www.easports.com/fifa/api/fifa16-ps4/stats/fut/Haggelodeon (maybe you need to hit the url twice to get data)
Ajax call:
$.ajax({
url: "https://www.easports.com/fifa/api/fifa16-ps4/stats/fut/Haggelodeon?callback=?",
dataType: 'jsonp',
success: function(data){
console.log(data);
}
});
I believe this is the code I tried, I deleted it in the meantime so there could be a mistake, especially with the url, I don't know if the callback-part is right from my memory.
I have a php script that I invoke via an ajax call with jQuery:
$.ajax({
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
type: "POST",
url: "getFares.php",
data: someObjectHere,
success: function(data) {
handleSuccess(data);
},
dataType: "json"
});
Most of the time this request works just fine and the server sends the values that I would expect.
Sometimes however, the server just responds with a 303 SEE OTHER response. Nowhere in my php script is anything that could produce this redirect.
Unfortunately I have not been able to find any kind of pattern when the redirect happens. It appears like it only happens when I load the page, then wait for a bit and then invoke the ajax request, but this might be coincidence.
I know this is not a very helpful description but unfortunately I'm stuck here so I'm hoping that someone by luck knows how to fix it.
Here are a couple of screenshots of the dev tools that should illustrate the problem more clearly:
In this image you can see that I've made a couple of request to the script (getFares.php) and I've highlighted one that worked. You can see the status code is 200 and everything is fine.
Here I've highlighted a failed request. The response is 303 See other. As you can see, none of the other values of the request have changed
The only difference in the requests that I have been able to find is seen here. For requests that return the correct result (status 200), the type is "xhr" and for requests that result in the redirect are of type "x-www-form-urlencoded; charset=UTF-8". I don't know why this happens or where it comes from.
I assume the problem could be related to faulty server/php settings but it's difficult to search for this kind of error.
It is not ajax issue but the server itself sending response for some reason.
getFares.php page must be checked for some post parameters, if fulfills, sends 200 response otherwise some other response like 303.
You can check getFares.php code to get more idea.
I recently started translating an old Classic ASP site to PHP. Several of its pages (Response.ContentType = "application/json") would just serve JSON responses such as {"R":1} and everything worked fine.
now on PHP, with header("content-type:application/json") first thing on the code, ajax just will not parse it. The client-side JS code is the same I used before. I haven't even touched it.
$.ajax({
dataType: "json",
type : "POST",
url: "processthisrequest.php",
cache: false,
async: false,
data: { Field1:"bla", Field2:"blabla"},
error: function(data){
// code on error
},
success: function(json){
// code on success
}
});
if the request is accepted ALL it serves is {"R":1} with double-quotes as it's always been.
Ajax will fire the error function no matter what. trying to debug it I found this:
readyState:4
responseText:{"R":1}
status:200
statusText:OK
searching for help I found a lot of people with problems with ajax getting readyState:4, status:200 and the response still not being parsed. none of the solutions worked or applied to my problem.
as it was working with IIS/ASP, can it be something with Apache or PHP?
UPDATE:
still no success, but if I get the server to serve a .js file with {"R":1} instead of processing a response through a php page ajax will fire the success function. which only proves my point that this is a php problem and also explains why it worked with old dinosaur ASP. now why PHP is not serving a proper mime-type is something I am trying to figure out.
I sorted it.
PHP has some very strange ways of doing things.
right after the header("Content-Type: application/json") line I have an include "file.php". although both the file that processes the Ajax request and the include file were saved as Unicode one had BOM signature and the other hadn't.
processing these two files made PHP send "something" back to the browser that was anything but a proper application/json response which wouldn't allow Ajax to parse it.
I know I'm supposed to stay on point here but I am so frustrated I need to add this comment: after wasting almost 18 hours of my life on this, time I could have used working on other things, the only thing I can learn from this experience is that free software is not free at all.
I have this function for getting a server id from a list. The function always returns "parsererror". I have looked at the JSON data returned but I cant seem to get it working, since jQuery have rewritten the ajax in v1.5.
function server_id()
{
$.ajax({
type: "GET",
url: "http://localhost/server_list.php",
dataType: "json",
success: function(data, status) {
alert(status + "\n\n" + data.server_id);
},
complete: function(data, status){
alert(status);
}
});
}
server_list.php
header('Content-type: application/json');
$output['server_id'] = '123';
print json_encode($output);
In firebug Net >> XHR it reads it as JSON as it brings up the tab and the Response tab shows what is below.
{"server_id":"123"}
I have also tried setting the content type header like below but having no luck.
Content-type: application/json
UPDATED
I only get "parsererror" if the validation plugin is loaded from http://bassistance.de/jquery-plugins/jquery-plugin-validation docs.jquery.com/Plugins/Validation v1.7.
If you add the plug jquery automatically adds the jsonp callback to the query string even when you set to false or dont include the parms for jsonp. Very Strange
Any ideas on how to fix?
Thanks
The simple solution here seems to be that jQuery 1.5 is not compatible with 1.7 of the validation plugin. Downgrading to jQuery 1.4.x (or otherwise patching or removing the validation plugin code as philhag suggested) solves the issue.
Huge thanks to those on this thread who identified the conflict. It saved me a bunch of headaches having to debug the jQuery code.
You seem to want regular json communication (dataType is "json" instead of "jsonp" and server_list.php sends json), but you're setting jsonp options. Remove the jsonp and jsonpcallback lines. Setting jsonp to false does not mean you disable it!
When these two lines are commented out, everything seems to work fine.
I suffered for days before finding this thread, thanks to those who pointed at jQuery.validate as the culprit.
In my testing it actually seems to be jquery.validate-vsdoc.js which is causing the issue, not the plugin itself, in case that's of any use to anyone else.