ajax request not working in localhost wampserver

13.3k views Asked by At

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?

4

There are 4 answers

2
Rakesh Nallam On BEST ANSWER

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"

1
cssyphus On

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

2
user3072843 On

You should put the URL instead of a relative path, e.g url: "http://localhost/something/profile/companyAutocomplete.php".

You can also omit the domain name, starting with "/something/profile/companyAutocomplete.php".

0
Raju On

Due to configuration differences between localhost and server, possible that it may be looking in local host at "http://localhost:port/path/file.php" instead of "http://localhost:port/webapproot/path/file.php". Changing your Ajax call in local host prepending web app name in relative path might resolve if it was the issue. Usually on hosting servers "/file.php" refers to application's root and so no issue, but it could be possible in local host that it may be looking at local server root by default since configuration. Better always to use relative path "/webappname/path/file.php", however use "http://127.0.0.1:port/webappname/path/file.php" which pretty faster instead of using "localhost" in local URLs.