简体   繁体   English

如何让Jquery发送POST数据来填充PHP中的$ HTTP_RAW_POST_DATA var?

[英]how to get Jquery to send POST data to populate $HTTP_RAW_POST_DATA var in PHP?

I am trying to tidy up some legacy code. 我正在尝试整理一些旧代码。

The following is a cut down of the main parts of the code that does the ajax request: 以下是执行ajax请求的代码的主要部分的简要说明:

var xmlHttp;
    try {
    // Firefox, Opera 8.0+, Safari
            xmlHttp=new XMLHttpRequest();
    } catch (e) {
    // Internet Explorer
            try {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                    try {
                            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e) {
                            $j.prompt("Your browser does not support AJAX!");
                    return;
                    }
            }
    }
    xmlHttp.onreadystatechange=function(){
            if(xmlHttp.readyState==4){
            // Get the data from the server's response
                // returns json data
                  //i know, don't ask
                eval(xmlHttp.responseText);

                // doing stuff with the json data
                ...
                etc
            }
    }
    xmlHttp.open("POST","fetchData.php",true);

    //sample data passed
    var request ="51.5&-0.12";
    xmlHttp.send(request);

I am trying to convert to: 我正在尝试转换为:

    $j.ajax({
        type: "POST",
        url: "fetchData.php",
        success: function(data) {
            mapFn._showSidebarHotels(data);
        },
        cache: false,            
        data: "51.5&-0.12",
        dataType: "json",
        processData: false
    })

The legacy code populates the $HTTP_RAW_POST_DATA var which the legacy backend code (that I prefer not to change) uses. 遗留代码填充$ HTTP_RAW_POST_DATA var,遗留后端代码(我不希望更改)使用该变量。

When using the jquery call, $HTTP_RAW_POST_DATA doesnt seem to get populated and what weird is that the $_POST[] seems to be empty as well. 使用jquery调用时,似乎没有填充$ HTTP_RAW_POST_DATA,并且奇怪的是$ _POST []也似乎为

I noticed that the difference in the POST tab of firebug when comparing the two ajax calls is that the jquery version has "application/x-www-form-urlencoded" as further info where as the legacy call doesnt. 我注意到,在比较两个ajax调用时,firebug的POST选项卡中的差异在于,jQuery版本具有“ application / x-www-form-urlencoded”作为进一步的信息,而传统调用则没有。

I believe the problem is down to the type of headers sent with the jquery ajax request but I'm not sure how further can I manipulate these using jquery. 我相信问题归结于与jquery ajax请求一起发送的标头的类型,但是我不确定如何进一步使用jquery操作这些标头。

My questions are: 我的问题是:

  • Is my jquery call correct? 我的jQuery调用正确吗? Given I have an empty $POST array 鉴于我有一个空的$ POST数组
  • How do I populate the $HTTP_RAW_POST_DATA using the parameters of jquery? 如何使用jquery的参数填充$ HTTP_RAW_POST_DATA?

The data is converted into a query string before it is sent. 数据在发送前被转换成查询字符串。 This processing can be circumvented by setting processData to false. 可以通过将processData设置为false来避免此处理。

Example: 例:

Sends an xml document as data to the server. 将xml文档作为数据发送到服务器。 By setting the processData option to false, the automatic conversion of data to strings is prevented. 通过将processData选项设置为false,可以防止将数据自动转换为字符串。

var xmlDocument = [create xml document];
$.ajax({
  url: "page.php",
  processData: false,
  data: xmlDocument,
  success: handleResponse
});

So did you try to change the content-type? 那么,您是否尝试过更改内容类型? Perhaps setting it equal to an empty string (as you say the legacy call didn't provide a content-type)? 也许将其设置为一个空字符串(正如您所说的旧调用未提供内容类型)?

I had a similar issue where the server was expecting the raw post data to be JSON. 我在服务器期望原始发布数据为JSON时遇到了类似的问题。 After changing the content-type to 'application/json' it worked fine. 将内容类型更改为“ application / json”后,它可以正常工作。

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

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