Skip to content Skip to sidebar Skip to footer

How Do You Send A Raw Strings Through A Jquery Post Request?

I am trying to POST a raw string via jQuery.ajax() e.g., contact_list=352345 I have $.ajax({ beforeSend: function(xhr){ xhr.setRequestHeader('Content-Type', he

Solution 1:

I would rather send a flag to my javascript function to see what sort of input

so your function will look like this

functionprocessIt(request_type,link,type,data) {
if(request_type == 'json')
$.ajax({
    beforeSend: function(xhr){
        xhr.setRequestHeader('Content-Type', header);
    }
    },
    url: link,
    type: type,
    processData:false,
    data: data,
    success: function(data){
        console.log(data);
    }
 });
else 
 $.ajax({
url: link,
data: data,
success: function(data){
    //do something
}
 });
}

But that's if you want to use your function as it is.

Solution 2:

No extra work needed. Try this:

$.ajax({
    url: link,
    data: data,
    success: function(data){
        console.log(data);
    }
 });

docs:

processDataBoolean:

Default: true By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

Solution 3:

PHP $HTTP_RAW_POST_DATA is not available with enctype="multipart/form-data". http://www.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data

So, now on the server I do the following:

if (count($_POST == 0){
    $raw = file_get_contents('php://input');    
}else{
    $raw = '';
    foreach($_POSTas$key => $value) {
        $raw = $raw.$key.'='.$value.'&';
    }
}

Seems to be working.

Post a Comment for "How Do You Send A Raw Strings Through A Jquery Post Request?"