ajax request not working in localhost wampserver
Solution 1
Thanks for your answers. It was not because of the relative URL reference.
I used the following function to figure out which error was causing the Ajax request to fail.
$(function() {
$.ajaxSetup({
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
});
The error was a parse error which was getting generated as the browser was trying to print something else before returning the JSON. It was fixed by using ob_start and ob_end_clean right before outputting the JSON which clears the buffer by getting help from the following link "dataType: "json" won't work"
Solution 2
The most common problem is attempting to access the page incorrectly on localhost.
With WAMP, XAMPP, etc you cannot just type in the address bar: c:\website\index.php
Instead, you must type: localhost
See this answer for more info:
Unable to load assets when testing website localy
Rakesh Nallam
Updated on June 04, 2022Comments
-
Rakesh Nallam almost 2 years
I have the following code
$.ajax({ url: "../profile/companyAutocomplete.php", type: "GET", data: dataQuery, dataType: "json", success: function(responseData) { companySelectHandler(responseData); } });
which i getting called in the production server without any issues. But when I used it in my local system, the ajax request is not made. I have tried in all the browsers but its still the same. What would be the causing the issue? I need to print the ajax error message for the same. How can i achieve that?
-
Jay Blanchard almost 9 yearsHave you watched the request / response in your browser's console?
-
william.taylor.09 almost 9 yearsCheck your webserver's config. It's entirely possible that it's not set up to handle the provided URL.
-
user2182349 almost 9 yearsAre you running it as file:// or http://?
-
AshBringer almost 9 yearsMay be cross domain request issue
-
RiggsFolly almost 9 yearsThis is most likely because your dev environment does not exactly match you live environment. Setup a Virtual Host so that the address you are using matches the live environment. See this for help
-
-
Iqbal Rizky almost 9 yearsit will not efficient if we use full URL, cause we will change all of URL when move from hosting to local server
-
m3nda about 7 yearsThis comment says in fact that you can't use
../
not just about relative, and because removing the domain converts it into relative this comment becomes unnacurate and in fact, false. -
m3nda about 7 yearsIndeed. A curious thing on that is that if you open the file with file:///etc you will see the xhr request from the jQuery method pointing to the right url (case you use absolutes) and it will return code 200, but the resources itself will not be listed, nor will work at all.
-
another over 6 yearsI have an Ajax request which doesn't work, so I am using your code, but it doesn't work neither. So it seems like it doesn't go into the success nor into the error.... Any idea? thanks.
-
trainoasis over 5 yearsThis comes in handy more times I care to admit :)