简体   繁体   English

Ajax值无法达到删除功能

[英]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. 我有这个Ajax代码,我从一个输入字段获取一个ID,并试图通过删除请求将其发送。

$("#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 . 我试图echo $id以查看该值是否到达函数,但它仅回显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 . PHP完全不知道名字questionid To fix this, use an object as your request data. 要解决此问题,请使用一个对象作为您的请求数据。

In your JS, try this 在您的JS中,试试这个

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

And then in your $.ajax call: 然后在您的$.ajax调用中:

data: reqdata,

I'm not sure that having "DELETE" in the type parameter of the ajax call is going to do what you want. 我不确定ajax调用的type参数中是否包含“ DELETE”会做什么。 Unless I miss my guess, it's expecting "GET", "POST", or "PUT". 除非我错过我的猜测,否则期望的是“ GET”,“ POST”或“ PUT”。 (The parameter 'type' is an alias for 'method'.) (参数“类型”是“方法”的别名。)

However, you could try this in the ajax call. 但是,您可以在ajax调用中尝试此操作。

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

try 尝试

data: {questionid: questionid},

in ajax request 在ajax请求中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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