简体   繁体   中英

HTTP POST Request with XML data

I am looking to make HTTP POST Request with XML payload.

I have looked at this documentation: http://api.jquery.com/jquery.post/

Here the parameter data is I believe I should be interested in.

However, it is not entirely very clear to me how do I specify the format of this data parameter. In most of my earlier experience this parameter is application/x-www-form-urlencoded

However, my request has an XML payload.

Any pointers ?

$.post is a shorthand function that covers some common usecases but has some unchangeable defaults. Use the $.ajax method instead. It allows you to set the content type for the request.

 contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8') 

Type: String

When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8" , which is fine for most cases. If you explicitly pass in a content-type to $.ajax() , then it is always sent to the server (even if no data is sent). The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding.

You can post XML data as

$.ajax({
url: ajaxurl,
data: "<test><node1></node1></test>", 
type: 'POST',
contentType: "text/xml",
dataType: "text",
success : parse,
error : function (xhr, ajaxOptions, thrownError){  
    console.log(xhr.status);          
    console.log(thrownError);
} }); 

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