次のような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>
たとえば、HttpWebRequest
オブジェクト、設定ヘッダー/メソッド/ ContentTypeを使用して、サーバー(.NET / C#)側でそれを行う方法を知っています。
しかし、私がそれをクライアント側でやりたいのなら?jQueryを使用したAjax?これらのパラメータはどこに設定できますか?
この機能を使用できます:
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);
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加