thuey
thuey

Reputation: 119

jQuery AJAX Post doesn't work

I can't figure out why the following code doesn't work. For the time being, the php function "alias_valid" just returns a string for testing purposes, so I haven't included the php function here. The alert is empty.

$.post(SITE_ROOT + "includes/AbstractEvent.php",{
    action:"alias_valid", 
    alias : alias},
    function(v){
        alert(v);
    });

Upvotes: 0

Views: 2416

Answers (3)

aziz punjani
aziz punjani

Reputation: 25766

There's no action argument in $.post. Try to concatenate it to the end of the url instead.

$.post(SITE_ROOT + "includes/AbstractEvent.php/alias_valid/",{ 
    alias : alias},
    function(v){
        alert(v);
    });

But i doubt this will work unless you are using an MVC framework. Or some kind of request routing that calls the specific function within the class.

Upvotes: 0

Oleg
Oleg

Reputation: 9359

My best guess is that one of your variables is undefined. Verify that SITE_ROOT and alias are indeed defined within your code. Also check if the SITE_ROOT ends in /.

The best way to test all that would be to launch your console (press F12) and see what errors you get. If there are none, you might be getting an empty response from the server.

Here's a working jsfiddle based on your example (again, press F12to access the output in the console).

Upvotes: 0

Sandeep Manne
Sandeep Manne

Reputation: 6092

If a request with jQuery.post() returns an error code, it will fail silently

Try .error() to check if there is any error happening

Syntax:

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post("example.php", function() {
  alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });

More info on $.post

Upvotes: 1

Related Questions