AJAX $ .ajax无法正常工作

This is my $.ajax function. volunteerDist is an array in a previous function and it calls myAjax(volunteerDis);However, the program always calls the error and complete functions, with an error message of undefined. What should I do? Thanks

admin-view-available-volunteeers.php is the filename where this is located volunteerDist is an array that contains floats

function myAjax(volunteerDist){
        $.ajax({
            type:'POST',
            url: 'admin-view-available-volunteeers.php',
            data : ({
                    distance:volunteerDist
                    }),
            success: function(){
                 alert('worked');
                },
           error :function(err){ 
                    alert('nope :( ERROR: ' + err.ErrorMessage);
                },
           complete : function(){
                       alert('thanks');
           }
        });
}

I always get this a lot. In your admin-view-available-volunteers.php you need to make sure that you're outputting the right headers.

To do that you need to put this in your php before anything is output:

header("HTTP/1.01 200 OK");
header("Content-type: text/html");

Otherwise it is returned as a 404 to jQuery's ajax and then goes into the error and complete bits.

If your error: handler is being called, then the remote script returned an error.

Fix the script, not the JS code!

To get better debugging on error you need to update your code, this

error :function(err){ 
    alert('nope :( ERROR: ' + err.ErrorMessage);
},

will not work - as err is a jqXHR object!

change it to this :

error: function(jqXHR, textStatus, errorThrown) {
   alert("Error : " + errorThrown);
}

This will output the message sent by the server.

All the details for the params of .ajax() are documentation here

Update

Check the name of your PHP file ...

admin-view-available-volunteeers.php 

has 3 es in the word volunteer ... is this the problem ?

You may wish to try loading a different test file first, something simple that way you can test the js separately and ensure that when you hit the php script directly in your browser that there are no errors shown.

Also in the future make sure you have all these ajax calls on the same domain as your php scripts(like you do now), otherwise ajax won't work across domains without adding a "Access-Control-Allow-Origin", "*" header.