0

This is my first use of Ajax. Here is the script I am trying to run:

$(document).ready(function() {  
    alert("on document ready");
    $(document).click(function(e){
        alert("inside click handler");
        $.get("runner.php", function(data) {
           alert("get method has completed");
        });
    }); 
});

The alerts: "on document ready" and "inside click handler" all display correctly but I cannot get the alert inside the $.get method to display. Also, the runner.php file is in the same directory as the JavaScript file. I eventually want to get information from the runner.php file but I cannot even make this $.get method complete.

I'm sure I'm missing something simple here, but this example was taken almost directly from the jQuery manual.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nathaniel Wendt
  • 1,194
  • 4
  • 23
  • 49
  • What do you intend `function(data)` to do? – Trojan Jul 31 '13 at 20:40
  • Check your browser console. Does it request the URL? What does it return? – orhanhenrik Jul 31 '13 at 20:40
  • Pop open your dev. console and see what it says. – tymeJV Jul 31 '13 at 20:40
  • If `runner.php` returns a failure, the alert will not display. What's inside the function is only executed when a success message is returned. – DevlshOne Jul 31 '13 at 20:41
  • trojansdestroy, I want the function to eventually return data that has queried a database. Jack, I am on a server. @Izzey I'm not quite sure where to look in the console to see if it requests. – Nathaniel Wendt Jul 31 '13 at 20:42
  • Check with firebug or ie developer toolbar if the url is right, and if so check the request and response, you have a net tab on both that trace the request – Daniele Jul 31 '13 at 20:42
  • If you are using `file://` instead of a real `http://` (by localhost or even live server) to make ajax, it WILL fail. – RaphaelDDL Jul 31 '13 at 20:44
  • I finally found it in my google developer console. The path was relative to the page that the script was loaded from. I through it would be relative to the js file itself. Thanks! – Nathaniel Wendt Jul 31 '13 at 20:45

5 Answers5

1

Try placing an absolute url to runner.php, so /js/runner.php or /runner.php whichever directory it's in.

Also if you have firebug installed, you can use it to view what the problem is. If it's telling you 404 Not Found then you will need to place the absolute url as I just mentioned.

Samer
  • 973
  • 1
  • 6
  • 15
1

Try to follow this pattern:

$.get("runner.php", function() {
  alert("success");
})
.done(function() { alert("second success"); })
.fail(function() { alert("error"); })
.always(function() { alert("finished"); });
zs2020
  • 53,766
  • 29
  • 154
  • 219
1

Where are these files? Are they on a web server or on your local computer? I had the same problem before and the reason being I was trying to access a file that was located on my local computer so I would always get an error

AJAX request using jQuery does not work

I fixed it by setting up a local web server and the same code worked beautifully :)

Community
  • 1
  • 1
barrigaj
  • 371
  • 2
  • 12
1

If you are using jquery 1.8 or later, then you need to use the fail() function to report errors with your call to your PHP, like this:

.get("runner.php", function() {
    alert("success");
})
.fail(function() { 
    alert("An error happened, check PHP script for problems."); 
})

Note: jQuery 1.8 deprecated the error callback and replaced it with the .fail() function.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
0

The most likely cause of nothing happening on the callback is a 404 or 500 error. As with a few other answers, make sure to add a fail listener to your $.get request and you can console.log(data); to determine what's going wrong on the server side.

FoleyX90
  • 1
  • 3