Cardiner
Cardiner

Reputation: 87

jQuery ajax throwing error

i need to send a request to my controler, but ajax throwing and error. I can't seem to figure out why. Here is the code

HTTPS is before defined constant, which i cannot write here!

        $("input").on("click", function(){
         var elem = $(this).attr("id");
         var execute;

         if( $(elem).prop('checked') == true ){
             execute = '+';
         } else {
             execute = '-';
         }
            $.ajax({
                type:'POST',       
                url: HTTPS+'/path/to/controller/ctlAccess.php?do='+execute,
                data: {id:elem},
                success: function(data) {
                            console.log(data);
                        },
                error: function(){
                            console.log("An error occurred: " + status + "nError: " + error); //AND ERROR: An error occurred: [object Object]nError: error
                        }
            });
        });
    });

And here is the controller:

    $SysData = new SysTables;

if ($_GET["do"] == "-") {
    $userId = preg_match('/[0-9]*(?=p)/', $_POST['id']);
    $pageId = preg_match('/(?<=p)[0-9]*/', $_POST['id']);
    $result = $SysData->deleteAccess($userId, $pageId);

$data = "ACCESS FOR ". $_POST['id']." DELETED";
    echo $data;
}

I think that requests do not even get to controler! I don't actually know hwere is the error. But path to file is right, and part where elem gets ir value works too, so i dont get where is the error!? Help please, thx

Upvotes: -1

Views: 599

Answers (2)

MonkeyZeus
MonkeyZeus

Reputation: 20737

You should probably specify the expected dataType in your AJAX call like this:

dataType: 'HTML'

I would also recommend switching to ALL POST data

So here is the whole AJAX thing (change your controller on the back-end to use $_POST['do']):

$.ajax({
    type:'POST',       
    url: HTTPS+'/path/to/controller/ctlAccess.php',
    data: {
               'id':elem,
               'do':execute // do might be a reserved word so just encase it in quotes to force it as a string
          },
    dataType: 'HTML',
    success: function(data) {
        console.log(data);
    },
    error: function(jqXHR, textStatus) {
        alert( "Request failed: " + textStatus );
    }
});

I would like to know what HTTPS is exactly. As long as your not trying to make a cross-domain request then you honestly shouldn't need it.

And if you are trying to make a cross-domain request then that is a separate beast on it's own.

Upvotes: 1

ErnestV
ErnestV

Reputation: 137

You're appending the "execute" value at the end of the URL. As "execute" is a plus sign this will be translated to a single space and possibly be discarded by jQuery as an invalid URL. You might try to make the content of "execute" something more readable.

You might also want to slightly modify your error handler, try this:

error: function( jqXHR, textStatus ) {
    alert( "Request failed: " + textStatus );
});

Upvotes: 0

Related Questions