我正在尝试在不刷新页面的情况下将视频文件上传到 youtube。我创建了简单的表单并尝试使用 ajaxSubmit 发送表单。
function prepareUploadForm() {
$('#upload').click(function() {
$('#upload').attr('disabled', true);
$('#upload').val('Uploading...');
var title = escapeXmlEntities($('#firstname').val()+" "+$('#lastname').val());
var description = escapeXmlEntities($('#firstname').val()+" "+$('#lastname').val()+' intro video');
var category = escapeXmlEntities('Sports');
var xmlBody = '<'+'?xml version="1.0"?>'+'<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007"> <media:group> <media:title type="plain">' + title + '</media:title> <media:description type="plain">' + description + '</media:description> <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">' + category + '</media:category> </media:group> </entry>';
$.ajax({
dataType: 'xml',
type: 'POST',
url: 'https://gdata.youtube.com/action/GetUploadToken',
contentType: 'application/atom+xml; charset=UTF-8',
headers: generateYouTubeApiHeaders(),
processData: false,
data: xmlBody,
success: function(responseXml) {
var xml = $(responseXml);
var nextUrl = window.location.href+"?task=user.youtubeFileUploadParse";
var submissionUrl = xml.find('url').text() + '?nexturl=' + encodeURIComponent(nextUrl);
var token = xml.find('token').text();
console.log("send data to: "+xml.find('url').text());
console.log(submissionUrl);
console.log('token:' +token);
submiss = submissionUrl;
$('#upload-form').attr('action', xml.find('url').text());
$('<input>').attr({
type: 'hidden',
name: 'token',
value: token
}).appendTo('#upload-form');
//implement ajax file upload to youtube with upload token
var options = {
url: submissionUrl,
success: function(response) {
console.log('Thanks for your file!');
}
};
$('#upload-form').ajaxSubmit(options);
// $('#upload-form').submit();
},
error: function(jqXHR) {
console.log('Metadata submission failed: ' + jqXHR.responseText);
$('#upload').removeAttr('disabled');
$('#upload').val('Upload');
}
});
});
$('#file-video').change(function() {
console.log('change video file');
if ($(this).val()) {
$('#upload').removeAttr('disabled');
} else {
$('#upload').attr('disabled', true);
}
});
$('#upload-form').show();
}
文件将成功上传,但我不希望将页面重定向到 nexturl 参数。如何拦截 youtube 响应标头并从中解析位置?