javascript - How do you send a raw strings through a jQuery POST request? - Stack Overflow

I am trying to POST a raw string via jQuery.ajax()e.g.,contact_list=352345I have$.ajax({beforeSend: fun

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', header);
        }
        },
        url: link,
        type: type,
        processData:false,
        data: data,
        success: function(data){
            console.log(data);
        }
     });

Most of the time I am sending JSON data, so header='application/json'

On the server side, I echo $HTTP_RAW_POST_DATA and see my JSON string just fine.

However, I sometimes want to send normal form data too. But when I set header='application/x-www-form-urlencoded' $HTTP_RAW_POST_DATA is empty.

ProcessData is false, so shouldn't it just pass my string on through?

As a temporary workaround I'm just leaving the header as application/json and ignoring the Content-Type on the server for this particular endpoint.

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', header);
        }
        },
        url: link,
        type: type,
        processData:false,
        data: data,
        success: function(data){
            console.log(data);
        }
     });

Most of the time I am sending JSON data, so header='application/json'

On the server side, I echo $HTTP_RAW_POST_DATA and see my JSON string just fine.

However, I sometimes want to send normal form data too. But when I set header='application/x-www-form-urlencoded' $HTTP_RAW_POST_DATA is empty.

ProcessData is false, so shouldn't it just pass my string on through?

As a temporary workaround I'm just leaving the header as application/json and ignoring the Content-Type on the server for this particular endpoint.

Share Improve this question asked Feb 5, 2012 at 7:13 spegspeg 2,0198 gold badges26 silver badges35 bronze badges 1
  • What is really boggling my mind, is that in the network inspector it looks like the request payload went through properly. e.g., contact_list=352345 is there... Maybe this is a PHP thing and form data doesn't show up in $HTTP_RAW_POST_DATA? – speg Commented Feb 5, 2012 at 14:46
Add a ment  | 

3 Answers 3

Reset to default 1

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

so your function will look like this

function processIt(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.

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.

PHP $HTTP_RAW_POST_DATA is not available with enctype="multipart/form-data". http://www.php/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($_POST as $key => $value) {
        $raw = $raw.$key.'='.$value.'&';
    }
}

Seems to be working.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745208693a4616715.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信