简体   繁体   中英

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:

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.

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.

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.

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.

My questions are:

  • Is my jquery call correct? Given I have an empty $POST array
  • How do I populate the $HTTP_RAW_POST_DATA using the parameters of jquery?

The data is converted into a query string before it is sent. This processing can be circumvented by setting processData to false.

Example:

Sends an xml document as data to the server. By setting the processData option to false, the automatic conversion of data to strings is prevented.

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. After changing the content-type to 'application/json' it worked fine.

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