简体   繁体   English

Rest Api PUT方法不接收值

[英]Rest Api PUT method not receiving values

I have this code in Ajax/Jquery, where I am trying to request a PUT method to my API. 我在Ajax / Jquery中有此代码,在这里我试图向我的API请求PUT方法。 In my code I am trying to send the data that has been updated in my text fields. 在我的代码中,我试图发送在文本字段中已更新的数据。 This data should be received by the API and send it to the Model it should be used to update a records in my database based on the ID recived. 该数据应该由API接收,然后将其发送到模型,该模型应用于根据接收到的ID更新数据库中的记录。 For some reason, my REST API in the controller is not receiving the values from my text fields. 由于某些原因,控制器中的REST API无法从文本字段接收值。

Code where I am sending the data: 我发送数据的代码:

$("#edit").on("click", function() {
     var updatedData ={ question: $('#questionField').val(),
                        image: $('#imageField').val(),
                        answer1: $('#answer1Field').val(),
                        answer2: $('#answer2Field').val(),
                        answer3: $('#answer3Field').val(),
                        answer4: $('#answer4Field').val(),
                         };
      $.ajax({
              url : '../admincontroller/getdatabasedata/', 
              type: 'PUT',
              dataType: 'json', 
              data: updatedData,

              success: function(updatedQuestion)
              {
                JSON.stringify(updatedQuestion);
                if(updatedQuestion.ok == 1) {
                  alert("Succesfully edited");
                }
                else{
                  alert(updatedQuestion.ok);
                }
              }
          });
    return false; 
  });

Code for my REST API where data should be received: 我的REST API的代码,应在其中接收数据:

    else if ($this->input->server('REQUEST_METHOD') == 'PUT' )
            {
                    $id = $this->input->post('idField');
                    $question = $this->input->post('questionField');
                    $image = $this->input->post('imageField');
                    $answer1 = $this->input->post('answer1Field');
                    $answer2 = $this->input->post('answer2Field');
                    $answer3 = $this->input->post('answer3Field');
                    $answer4 = $this->input->post('answer4Field');

if($question != '' && $image != '' && $answer1 != '' && $answer2 != '' && $answer3 != '' && $answer4 != ''){
                        $this->adminmodel->updateQuestion($id,$question,$image,$answer1,$answer2,$answer3,$answer4);
                        echo json_encode(array('ok' => 1));
                    }
                    else{
                        echo json_encode(array('ok' => $question));
                    }   

            }

At the moment I am trying to alert to see if the data is passed, but I just get NULL. 目前,我正在尝试提醒是否传递了数据,但我只是得到NULL。 Any idea why? 知道为什么吗? Thanks. 谢谢。

I think the data needs to be a String. 我认为数据必须是字符串。 Objects are converted to querystrings which is what you are seeing here. 对象将转换为查询字符串,这就是您在此处看到的。

data: JSON.stringify(updatedData) instead of updatedData 数据:JSON.stringify(updatedData)而不是updatedData

Follow similar data contract structure and parameter names. 遵循类似的数据协定结构和参数名称。 If you are using primitive types pass them in url or mark the primitive type of sent in body in the api function. 如果您使用的是原始类型,请在url中传递它们,或在api函数中标记body中发送的原始类型。 Use JSON.stringify(object) as data for Ajax call for sending objects. 使用JSON.stringify(object)作为用于发送对象的Ajax调用的数据。 Make sure http verbs are enabled on server. 确保在服务器上启用了HTTP动词。

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

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