Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我怎样才能定制;邮政「;客户端的请求?_Javascript_Ajax_Http_Post_Request - Fatal编程技术网

Javascript 我怎样才能定制;邮政「;客户端的请求?

Javascript 我怎样才能定制;邮政「;客户端的请求?,javascript,ajax,http,post,request,Javascript,Ajax,Http,Post,Request,我需要做一个POST请求,例如: 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 <?x

我需要做一个POST请求,例如:

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>
POST/feeds/api/users/default/subscriptions HTTP/1.1
主持人:gdata.youtube.com
内容类型:应用程序/atom+xml
内容长度:内容长度
授权:承载访问\u令牌
GData版本:2
X-GData-Key:Key=DEVELOPER\u Key
谷歌开发者
我知道如何在服务器(.NET/C#)端执行,例如,使用
HttpWebRequest
object、settings头/Method/ContentType


但如果我想在客户端做的话?Ajax与jQuery?在哪里可以设置这些参数?

您可以使用此功能:

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
    });
}
使用如下代码:

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);
});
var请求=“”+
'' +
'    '+
“谷歌开发者”+
'';
变量头={
“内容类型”:“应用程序/atom+xml”,
“授权”:“承载访问\u令牌”
“GData版本”:2
“X-GData-Key”:“Key=DEVELOPER\u Key”
};
post('/some/url',请求,标题,函数(响应){
警报(响应);
});

似乎工作完美,即使youtube回复401错误!但错误在别处!非常感谢。