简体   繁体   中英

Ajax value can't reach delete function

I have this Ajax code where I am getting an ID from a input field and I am trying to send it via a delete request.

$("#removebutton").on("click", function() {
      var questionid = $('#questionid').val();
      $.ajax({
              url : '../admincontroller/getdatabasedata', 
              type: 'DELETE',
              data: questionid,
              success: function(response)
              {
                JSON.stringify(response);
                alert(response.ok);
              }
          });
    return false; 
  });

Here is where the request is sent:

else if ($this->input->server('REQUEST_METHOD') == 'DELETE')
    {
        $id = $this->input->get('questionid');
        if($id != ''){
            $this->adminmodel->deleteQuestion($id);
            echo json_encode(array('ok' => $id));
        }
        else{
            echo json_encode(array('ok' => $id));
        }   

    }

I am trying to echo $id to see if the value reaches the function, but it only echo UNDEFINED . Why can't my function receive the value?

You're not sending your data as a key/value set. You're just passing a scalar which is probably interepereted as a string. The PHP is completely unaware of the name questionid . To fix this, use an object as your request data.

In your JS, try this

var reqdata = {questionid: $('#questionid').val() }

And then in your $.ajax call:

data: reqdata,

I'm not sure that having "DELETE" in the type parameter of the ajax call is going to do what you want. Unless I miss my guess, it's expecting "GET", "POST", or "PUT". (The parameter 'type' is an alias for 'method'.)

However, you could try this in the ajax call.

$.ajax({
    url: '../admincontroller/getdatabasedata',
    type: 'POST',
    data: {
        request: 'DELETE',
        questionId: questionId
    },
    success: function(response) {
        ....
    }
}

try

data: {questionid: questionid},

in ajax request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM