jQuery AJAX Post不起作用

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);
    });

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

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.

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