简体   繁体   English

如何在PHP中读取XmlHttpRequest PUT请求的值

[英]How can I read the values of a XmlHttpRequest PUT request in PHP

The idea is to send a from via a PUT request like this : 这个想法是通过这样的PUT请求发送来自的:

    var oData = new FormData(document.getElementById("editPostForm"));

    var oReq = new XMLHttpRequest();
    oReq.open("PUT", "http://localhost/cms1/api2.php?type=post", true);
    oReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    oReq.onload = function(oEvent) {
        if (oReq.status == 200) {
            //postSaved();
            console.log(oReq.responseText);
        } else {
            console.log("Error " + oReq.status + " occurred uploading your file.<br \/>");
        }
    };

    oReq.send(oData);

This seems to be working. 这似乎正在工作。 The next part is catching oData on the serverside with this. 下一步是在服务器端捕获oData。

    switch($_SERVER['REQUEST_METHOD']):
        case "PUT":
        header('HTTP/1.1 201 Created');
        global $data;
        parse_str(file_get_contents("php://input"),$post_vars);
            switch($_GET['type']):
                case "post":
                    //$data['post'] = $post->editPost($post_vars);
                    break;
                case "comment":
                    //$data['comment'] = $comment->editComment($post_vars);
                    break;
            endswitch;
        //echo json_encode($data);
        echo json_encode($post_vars);//used for testing right now
        exit();
        break;
    endswitch;

For testing purposes i json_encoded the $post_vars which gives me the following output in chrome console : 出于测试目的,我对$ post_vars进行了json_encoded,这在chrome控制台中提供了以下输出:

Array
(
[------WebKitFormBoundaryRyXIibrT3VkbWXMz
Content-Disposition:_form-data;_name] => "post_id"
52

------WebKitFormBoundaryRyXIibrT3VkbWXMz
Content-Disposition: form-data; name="post_type"
car stolen

------WebKitFormBoundaryRyXIibrT3VkbWXMz
Content-Disposition: form-data; name="post_location"
somnsomnsomn

------WebKitFormBoundaryRyXIibrT3VkbWXMz
Content-Disposition: form-data; name="upload"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryRyXIibrT3VkbWXMz
Content-Disposition: form-data; name="post_message"


<p>cant figure this out</p>
------WebKitFormBoundaryRyXIibrT3VkbWXMz--

)

The problem is when i want to get the value of the array like $post_vars['post_id'] i get the following error 问题是当我想获取像$ post_vars ['post_id']这样的数组的值时,出现以下错误

<b>Notice</b>:  Undefined index: post_id in <b>G:\server\htdocs\cms1\api2.php</b> on line <b>67</b>

I don't know if this is because of the FormDataObject, or the way i make the XMLHTTPRequest, or even the my (sort of) api is handling the requests. 我不知道这是因为FormDataObject,还是我制作XMLHTTPRequest的方法,甚至是我的(某种)api正在处理请求。

My question is: 我的问题是:

  • is there a way so make the XMLHTTPRequest in a way so I can use the keys to get the values out of the array in php (like $post_vars['key']) 有没有办法让XMLHTTPRequest以某种方式使我可以使用键从php数组中获取值(例如$ post_vars ['key'])
  • if there is no such way, how can I get the values out of the $post_vars array? 如果没有这种方法,如何从$ post_vars数组中获取值?

I experience no problems when I use a "GET" or "POST" request or when I use curls. 当我使用“ GET”或“ POST”请求或使用卷发时,我没有遇到任何问题。

ps.if somebody has a better idea, i'm open for that. ps。如果有人有更好的主意,我愿意接受。 jQuery ajax doens't accept formData and the jquery.ajax.plugin uses the same FormdataObject method I'm using so... please don't just post jQuery and flee the scene... jQuery ajax不接受formData,而jquery.ajax.plugin使用与我使用的相同的FormdataObject方法...请不要只是发布jQuery并逃离现场...

Instead of print_r($post_vars) use echo json_encode($post_vars); 代替print_r($post_vars)使用echo json_encode($post_vars); .

Then in your javascript you want to var response = JSON.parse(oReq.responseText); 然后在您的JavaScript中,您想要var response = JSON.parse(oReq.responseText);

After that you should be able to reference fields in the response variable. 之后,您应该能够引用响应变量中的字段。 response.post_id

Edit: 编辑:

This would be a lot easier with JQuery. 使用JQuery会容易得多。 Can you include the library? 你可以包括图书馆吗? Then add this code: 然后添加以下代码:

$.post(
    "http://localhost/cms1/api2.php?type=post",
    $("#editPostForm").serialize(),
    function(data){
        console.log(data);
    }, "json");
);

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

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