Java 使用prototype调用HTTP PUT真的有可能吗
我正在Java 使用prototype调用HTTP PUT真的有可能吗,java,ajax,rest,prototypejs,http-put,Java,Ajax,Rest,Prototypejs,Http Put,我正在NetBean6.9上运行JEE6和glassfishv3,并在restfulweb服务上工作 我有一个jsp文件,其中包含下面的javascript函数 它基本上从HTML输入字段中读取信息,并转换为JSON格式 然后使用onclick Ajax调用,尝试使用HTTP PUT方法发送JSON字符串。 (即,我正在尝试使用REST更新数据库记录) 对于js框架,我使用的是Prototype1.7 当我测试下面的函数时,它总是返回404,因此显示“出错”警报 根据我的搜索原型1.5以上版本支
NetBean6.9
上运行JEE6
和glassfishv3
,并在restfulweb服务上工作
我有一个jsp文件,其中包含下面的javascript函数
它基本上从HTML输入字段中读取信息
,并转换为JSON格式
然后使用onclick Ajax调用
,尝试使用HTTP PUT方法发送JSON字符串。
(即,我正在尝试使用REST更新
数据库记录)
对于js框架,我使用的是Prototype1.7
当我测试下面的函数时,它总是返回404,因此显示“出错”警报
根据我的搜索原型1.5以上版本支持HTTP PUT/DELETE方法,为此,向请求URL添加\u方法,就像我正在做的那样:
var url = "/resources/inventory/" + invId + "?_method=PUT";
这将创建例如:
http://localhost:8080/NoJSF/resources/inventory/123?_method=PUT
我查看了Firebug和console,发现请求实际上是POST。不确定,但我相信这是因为原型使用后隧道实现PUT方法
此外,尽管调用了Ajax,但我的带有JAX-RS注释的witn@POST的Java文件甚至没有被调用(@GET version正在处理单独的数据,因此这是正确的文件),因为它的方法的第一行spit消息没有显示,所以我怀疑我的Ajax语句有一些错误,或者有一些超出我的想法。。谁能给我一个提示吗
function protoAjaxPut() {
//get all fields value and store into json
var invId = document.getElementById("invIdField").value;
var invName = document.getElementById("invNameField").value;
//put info into JSON format
var jsonInput = JSON.stringify(new Array(invName));
var url = "/resources/inventory/" + invId + "?_method=PUT";
new Ajax.Request(url, {
method:'put',
postBody: jsonInput,
ContentType: 'application/json',
onSuccess: function(transport) {
var responseData = transport.responseText;
document.getElementById('putResponseText').innerHTML = responseData;
},
onFailure: function() { alert('something went wrong!')}
})
}//end protoAjaxPut
他们被挖出隧道:
下面使用原始xml而不是原型是可行的
当我使用prototype ajax调用时,405 method not allowed将返回,不知道为什么
<script type="text/javascript">
function simplePut() {
var xmlHttp = new XMLHttpRequest();
var invId = document.getElementById("invIdField").value;
var invName = document.getElementById("invNameField").value;
//put info into JSON format
var jsonInput = JSON.stringify(new Array(invId, invName));
var url = "resources/inventory/" + invId;
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
//out = xmlHttp.responseText;
document.getElementById('simple').innerHTML = xmlHttp.responseText;
}
}
xmlHttp.open("put", url, true);
//xmlHttp.open("put", "resources/inventory/1", true);
//xmlHttp.setRequestHeader("Content-Type", "text/plain");
xmlHttp.setRequestHeader("Content-Type", "application/json");
xmlHttp.send(jsonInput);
}//end protoAjaxPut
</script>
...html body
<body>
<h1>Inventory page</h1>
<table border="1" cellspacing="1" cellpadding="5">
<th>id</th>
<th>amount</th>
<c:forEach items="${inventoryList}" var="inv" >
<tr>
<td>${inv.id}</td>
<td><a href="" onclick="ajaxGet(${inv.id}); return false;">${inv.amount}</a></td>
</tr>
</c:forEach>
</table>
<hr />
<h3>REST</h3>
<form method="post" action="">
Inventory ID: <input type="test" id="invIdField" readonly /><br />
Inventory Name: <input type="text" id="invNameField" /><br />
<input type="button" value="insert POST form" onclick="protoAjaxPost()" /><br />
<!-- <input type="button" value="update PUT" onclick="protoAjaxPut()" /><br /> -->
<div id="putResponseText"></div>
</form>
<button onclick="protoAjaxPut()">update PUT</button><br />
<button onclick="simplePut()">call SIMPLE PUT</button><br />
<button onclick="ajaxDelete()">HTTP DELETE</button><br />
<div id="simple"></div>
</body>
函数simplePut(){
var xmlHttp=new XMLHttpRequest();
var invId=document.getElementById(“invIdField”).value;
var invName=document.getElementById(“invNameField”).value;
//将信息转换为JSON格式
var jsonInput=JSON.stringify(新数组(invId,invName));
var url=“资源/库存/”+invId;
xmlHttp.onreadystatechange=函数(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
//out=xmlHttp.responseText;
document.getElementById('simple').innerHTML=xmlHttp.responseText;
}
}
open(“put”,url,true);
//open(“put”,“resources/inventory/1”,true);
//setRequestHeader(“内容类型”、“文本/普通”);
setRequestHeader(“内容类型”、“应用程序/json”);
send(jsonInput);
}//终端协议输出
…html正文
库存页面
身份证件
数量
${inv.id}
休息
库存ID:
存货名称:
更新PUT
调用简单PUT
HTTP删除
如您在问题中所述,默认情况下。(包括我)认为这是愚蠢的行为,但由于开发人员似乎并不关心这一点,我们必须通过编辑请求函数本身(而不涉及prototype.js!):
在原型初始化后运行此代码。
现在:
new Ajax.Request('42', {method:'PUT'});
将导致实际的HTTP PUT请求()。我要感谢RienNeVaPlus的回答。我添加了额外的覆盖,以便使用参数和contenttype正确发布:
Ajax.Request.prototype.setRequestHeaders = function() {
var headers = {
'X-Requested-With': 'XMLHttpRequest',
'X-Prototype-Version': Prototype.Version,
'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
};
if (this.method == 'post' || this.method=='put') {
headers['Content-type'] = this.options.contentType +
(this.options.encoding ? '; charset=' + this.options.encoding : '');
/* Force "Connection: close" for older Mozilla browsers to work
* around a bug where XMLHttpRequest sends an incorrect
* Content-length header. See Mozilla Bugzilla #246651.
*/
if (this.transport.overrideMimeType &&
(navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005)
headers['Connection'] = 'close';
}
if (typeof this.options.requestHeaders == 'object') {
var extras = this.options.requestHeaders;
if (Object.isFunction(extras.push))
for (var i = 0, length = extras.length; i < length; i += 2)
headers[extras[i]] = extras[i+1];
else
$H(extras).each(function(pair) { headers[pair.key] = pair.value });
}
for (var name in headers) {
this.transport.setRequestHeader(name, headers[name]);
}
};
Ajax.Request.prototype.setRequestHeaders=function(){
变量头={
“X-request-With':“XMLHttpRequest”,
“X-Prototype-Version”:Prototype.Version,
“接受”:“text/javascript,text/html,application/xml,text/xml,*/*”
};
if(this.method='post'| | this.method=='put'){
标题['Content-type']=this.options.contentType+
(this.options.encoding?';字符集='+this.options.encoding:“”);
/*强制“连接:关闭”让旧版Mozilla浏览器工作
*围绕XMLHttpRequest发送错误消息的错误
*内容长度标题。参见Mozilla Bugzilla#246651。
*/
如果(this.transport.overrideMetype&&
(navigator.userAgent.match(/Gecko\/(\d{4})/)| |[02005])[1]<2005)
标题['Connection']='close';
}
if(typeof this.options.requestHeaders=='object'){
var extras=this.options.requestHeaders;
if(Object.isFunction(extras.push))
对于(变量i=0,长度=extras.length;i
只测试了PUT方法。是的,谢谢,我已经访问了那个网站:)我的代码中也有一些让我困惑的错误。实际上我忘记了我的问题是关于原型的,哎哟,但只要PUT在某种程度上起作用就行了。
Ajax.Request.prototype.setRequestHeaders = function() {
var headers = {
'X-Requested-With': 'XMLHttpRequest',
'X-Prototype-Version': Prototype.Version,
'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
};
if (this.method == 'post' || this.method=='put') {
headers['Content-type'] = this.options.contentType +
(this.options.encoding ? '; charset=' + this.options.encoding : '');
/* Force "Connection: close" for older Mozilla browsers to work
* around a bug where XMLHttpRequest sends an incorrect
* Content-length header. See Mozilla Bugzilla #246651.
*/
if (this.transport.overrideMimeType &&
(navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005)
headers['Connection'] = 'close';
}
if (typeof this.options.requestHeaders == 'object') {
var extras = this.options.requestHeaders;
if (Object.isFunction(extras.push))
for (var i = 0, length = extras.length; i < length; i += 2)
headers[extras[i]] = extras[i+1];
else
$H(extras).each(function(pair) { headers[pair.key] = pair.value });
}
for (var name in headers) {
this.transport.setRequestHeader(name, headers[name]);
}
};