我怎样才能让一个自定义的" POST"要求在客户端? [英] How can I make a custom "POST" request on client side?
本文介绍了我怎样才能让一个自定义的" POST"要求在客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要做一个POST请求,如:
I need to do a POST request such as:
POST /feeds/api/users/default/subscriptions HTTP/1.1
Host: gdata.youtube.com
Content-Type: application/atom+xml
Content-Length: CONTENT_LENGTH
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=DEVELOPER_KEY
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<category scheme="http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat"
term="channel"/>
<yt:username>GoogleDevelopers</yt:username>
</entry>
我知道如何做到这一点的服务器(.NET / C#)侧,例如,使用的HttpWebRequest
对象,设置页眉/方法/ ContentType的。
I know how to do it server (.NET/C#) side for example, using HttpWebRequest
object, settings Header/Method/ContentType.
但是,如果我愿意做客户端?阿贾克斯与jQuery?我在哪里可以设置这些参数?
But if I'd like to do it client side? Ajax with jQuery? Where can I set those parameters?
推荐答案
您可以使用此功能:
function post(url, data, headers, success) {
$.ajax({
beforeSend: function(xhr){
$.each(headers, function(key, val) {
xhr.setRequestHeader(key, val);
});
xhr.setRequestHeader('Content-Length', data.length);
}
type: "POST",
url: url,
processData: false,
data: data,
dataType: "xml",
success: success
});
}
使用code是这样的:
using code like this:
var request = '<?xml version="1.0" encoding="UTF-8"?>' +
'<entry xmlns="http://www.w3.org/2005/Atom"' +
' xmlns:yt="http://gdata.youtube.com/schemas/2007">' +
' <category scheme="http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat" term="channel"/>'+
' <yt:username>GoogleDevelopers</yt:username>' +
'</entry>';
var headers = {
'Content-Type': 'application/atom+xml',
'Authorization': 'Bearer ACCESS_TOKEN'
'GData-Version': 2
'X-GData-Key': 'key=DEVELOPER_KEY'
};
post('/some/url', request, headers, function(response) {
alert(response);
});
这篇关于我怎样才能让一个自定义的&QUOT; POST&QUOT;要求在客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文