huantian
huantian
  • 发布:2015-02-26 23:54
  • 更新:2015-05-04 18:08
  • 阅读:3117

XMLHttpRequest 支持put delete吗

分类:HTML5+
  • -暂时没4.4的手机,无法测试,求告知
2015-02-26 23:54 负责人:无 分享
已邀请:
anke

anke

同问,不支持 put delete请求吗?怎么解决这个问题?

DCloud_UNI_FXY

DCloud_UNI_FXY

如果使用的是mui的ajax。可以设置type:'put|delete'

anke

anke

thx,解决了

hyhezhen

hyhezhen

因为ajax不方便显示服务器数据,所以这样

PHP端保存请求数据的提交类型到txt(get/post/delete/put)
<?php
$request_method = strtolower($_SERVER['REQUEST_METHOD']);
echo $request_method;

$l=fopen("log.txt","a+");
fwrite($l,$request_method."\r\n");
fclose($l);
?>
============================================
我用这样的函数调用 delete传参数
function demoDel()
{
var path = "http://192.168.1.156/learn/mydemo.php";
mui.ajax(path,{
data:{
pwd:'123',
},
dataType:'json',//服务器返回json格式数据
type:'put|delete',//HTTP请求类型
timeout:5000,//超时时间设置为10秒;
success:function(data){
},
error:function(xhr,type,errorThrown){
//异常处理;
console.log(type);
showWarning("出错"+type);
}
});
}

服务器为PHPSTUDY
电脑上直接用命令
curl -X DELETE 192.168.1.156/learn/mydemo.php
可以看到是 delete, 所以服务器可以接受 delete没问题

而手机上调用demoDel,则
无论type 是 put 还是 delete 还是 put|delete, 查看结果始终是 get,
请问我的写法有错误吗?

hyhezhen

hyhezhen

上面的情况,同样的代码(Hbuilder 基座调试),
iphone5s ios7 下, 显示提交类型为 get
红米note 安卓4.2.2 下,显示提交类型为 post
反复切换实验5次以上,结果都是一样。


请问有正确的方法,可以保证delete可以被正常提交识别吗?
谢谢

DCloud_UNI_FXY

DCloud_UNI_FXY

临时解决办法:

在调用非get|post请求时,加上:

mui.ajaxSettings.xhr = function() {  
                    return new window.XMLHttpRequest();  
                };
hyhezhen

hyhezhen

根据群主提供的参考代码,成功以 delete 方式提交数据。
解决了一个大问题。
感谢

document.getElementById("test").addEventListener('tap', function() {  
mui.ajaxSettings.xhr = function() {  
return new window.XMLHttpRequest();  
};  
mui.ajax('http://10.0.0.25:8081/admin/put', {  
type: 'PUT',  
success: function(response) {  
console.log(response);  
},  
error: function(xhr) {  
console.log(JSON.stringify(xhr));  
}  
});  
});  

document.getElementById("test1").addEventListener('tap', function() {  
mui.ajaxSettings.xhr = function() {  
return new window.XMLHttpRequest();  
};  
mui.ajax('http://10.0.0.25:8081/admin/delete', {  
type: 'DELETE',  
success: function(response) {  
console.log(response);  
},  
error: function(xhr) {  
console.log(JSON.stringify(xhr));  
}  
});  
});

该问题目前已经被锁定, 无法添加新回复