【发布时间】:2012-09-28 15:58:08
【问题描述】:
通过 AJAX,您应该支持“PUT”和“DELETE”请求。我有一个通过“PUT”请求发送数据的表单,服务器确认它但没有发送任何参数。 “删除”请求也会出现同样的结果。如果我改为“发布”,它工作正常。 IE 9、Firefox 和 Chrome 都产生相同的结果。正在发送 put 和 delete 请求,但没有数据。
$("#startButton").click(function(){
$.ajax({url:"http://localhost:8084/Project/servlet",
data:parseFormData("simulatorForm"),
cache: "false",
dataType: "text",
contentType: "application/x-www-form-urlencoded",
type:"put",
error:function(xhr){alert(xhr.status + xhr.statusText);} });
});
注意:如果我改为“发布”,则效果很好。我的表单中的所有参数都会被传输。我在 IE、Chrome 和 Mozilla Firefox 中尝试过。
我尝试在纯 javascript 中执行此操作,但得到完全相同的结果。
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("PUT","http://localhost:8084/UtilityDashboard/SensorSimulator",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(parseFormData("simulatorForm"));
【问题讨论】:
-
get或postmehtod 有什么问题 -
@Archer 这个问题是关于通过表单提交请求的。鉴于用户发布的答案,这应该是可行的。 XMLHTTPRequest 被认为是常规表单提交的解决方法。
-
@jhonraymos 没什么,但如果它有效,它在服务器端会更整洁。我正在使用apache tomcat。使用 post 进行更新和创建意味着向同一方法添加更多 if 语句。
-
能否也提供
parseFormData()?其他一切看起来都很好。
标签: javascript ajax jquery