0

I'm writing a basic API to learn how they work. It seems that using the DELETE method in the AJAX call prevents the passing of the value that was entered in the form along to the PHP script.

Here is the form:

    <legend>Remove Mobile</legend>
    <ol>
        <li>
            <label for="id">ID</label>
            <input type="text" name="id" id="id"></input>
        </li>
    </ol>
    <button type="button" onclick="deleteMobile()">Submit</button>                      
    <button type="reset">Cancel</button>
</form>

Here is the jQuery AJAX call:

function deleteMobile() {
    $.ajax({
        method:"POST",  // this needs to be DELETE for API, it is currently set to POST so form value passes to PHP script
        url:"glue.php",
        data: {id: $('input[name="id"]').val()},
        async: true,
        cache: false
    })
    .done(function(data){
        // log data to console for visibility
        console.log(data);
        // clear contents of form
        $('input[name="id"]').val('');
    })
    .fail(function(){
        console.log('AJAX call failed');
    })

Here is the glue.php script so far:

<?php
$id = isset($_POST['id']) ? $_POST['id'] : '';
header('Location: http://localhost:8080/miscellaneous/APIexamples/CRUD/mobile/' . $id);
?>

When these two lines are added to glue.php before header(...

echo ($_SERVER['REQUEST_METHOD']);
exit($id);

then POST and whatever value was inputted into the form are displayed in the browser console. However, when the AJAX method is changed to DELETE then DELETE appears in the browser console but not the form value. How can the form value be passed to the PHP script when the DELETE method is used in the AJAX call?

knot22
  • 2,648
  • 5
  • 31
  • 51
  • 1
    This seems to have been answered here: https://stackoverflow.com/a/15089299/378779 – kmoser Sep 08 '19 at 23:30
  • I ended up using the approach posted by @ow3n on 4/1/2018 in this thread https://stackoverflow.com/questions/8032938/jquery-ajax-put-with-parameters. – knot22 Sep 09 '19 at 00:09

1 Answers1

-1

I think your request is failing during CORS (OPTIONS method) and that's why You are not seeing form data in debugger. That has nothing to do with Jquery Ajax. Check if You are really seeing DELETE and not OPTIONS.

chojnicki
  • 3,148
  • 1
  • 18
  • 32
  • When I do `echo ($_SERVER['HTTP_ORIGIN']);` the console displays `Undefined index: HTTP_ORIGIN in ...glue.php` so it is not a CORS request. – knot22 Sep 08 '19 at 22:36