2

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?

John Conde
  • 217,595
  • 99
  • 455
  • 496
Rakesh Nallam
  • 235
  • 2
  • 6
  • 17

4 Answers4

5

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"

Community
  • 1
  • 1
Rakesh Nallam
  • 235
  • 2
  • 6
  • 17
  • 1
    I 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. – another Aug 30 '17 at 10:47
  • This comes in handy more times I care to admit :) – trainoasis Aug 23 '18 at 06:21
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

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Indeed. 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. – m3nda Feb 16 '17 at 18:54
0

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.

Raju
  • 75
  • 10
-1

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".

user3072843
  • 308
  • 3
  • 13
  • it will not efficient if we use full URL, cause we will change all of URL when move from hosting to local server – Iqbal Rizky Jun 13 '15 at 00:30
  • This 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 Feb 16 '17 at 18:46