我正在尝试制作一个按钮,该按钮在上传映像时会使用AJAX进行OPLOAD。但是成功回调无法正常工作(我可以说,因为甚至无法在console.log
中显示其内容。我做错了什么?
我更改了URL零件,现在iḿ在控制台上接收到:http://192.168.0.229:8000/feed/add 419(未知状态)
$(document).on('click', '[data-type-image]', function () {
$('#image_upload').click();
var formData = new FormData($(document));
$.ajax({
url: window.location.href,
type: 'POST',
data: formData,
beforeSend: function () {
console.log('before send working');
$('form').find('div').find('button').attr('disabled', true);
$('form').find('div').find('button').css('background-color', 'grey');
},
success: function(){
console.log('success working');
$('form').find('div').find('button').attr('disabled', false);
$('form').find('div').find('button').removeAttr('style');
},
error: function () {
console.log(error);
},
cache: false,
contentType: false,
processData: false,
xhr: function () { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) { // Avalia se tem suporte a propriedade upload
myXhr.upload.addEventListener('progress', function (e) {
}, false);
}
return myXhr;
}
});
});
$(document)
返回jQuery对象,而是要传递到ajax url属性一个包含端点URL的字符串。假设您想在同一端点中执行POST
请求,请返回按钮的视图。
替换
url: $(document),
url: window.location.href,
编辑:(错误从500更改为419)
AJAX成功回调需要成功的HTTP响应(例如200,302 ...)。如果后端在POST
请求后未向客户端发送成功响应,则预计将收到419错误:存在端点,但是POST
请求没有返回操作,仅对GET
返回视图。
您尚未为Ajax提供有效的URL。请参阅文档:http://api.jquery.com/jquery.ajax/URL应该是有效的URL字符串。您正在通过 $(文档),这是一个对象。查看开发工具中的"网络"选项卡,是否要使用服务器。
这是代码可以为您提供索引,但首先您需要选择单词。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(
function(){
$("#id1").click(function(){
var txt = $("#id1").val();
var test= txt.split(" ")
$(test).each(function(item){
if(test[item]==window.getSelection()){
alert( item);
}
});
});
});
</script>
<input type="text" id="id1" value="10" />