javascript jQuery $.ajax() 执行了两次?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5544422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery $.ajax() executed twice?
提问by abolotnov
Here is a button:
这是一个按钮:
<input type="button" value="add to cart" id="addToCart" />
and a bound event:
和绑定事件:
$("#addToCart").bind('click',function(){
$.ajax({
url: '/cartManager/add',
data:{
pictureId: currentImageId,
printSize: $("#size option:selected").val(),
paperType: $("#paperType option:selected").val(),
quantity: 1
},
success: function(){
$("#modal").html("<h1>ОК</h1><p>Closing in a sec</p>").delay(1000);
$("#modal").overlay().close();
}
});
return false;
});
And everything works find apart one thing that kind of bothers, I see two requests in Chrome dev console for this:
一切正常,发现一件令人烦恼的事情,我在 Chrome 开发控制台中看到了两个请求:
- add /cartManager:
- 添加/购物车管理器:
Request URL:http://127.0.0.1:8000/cartManager/add?pictureId=4&printSize=2&paperType=1&quantity=1 Request Method:GET Status Code:301 MOVED PERMANENTLY
Request URL:http://127.0.0.1:8000/cartManager/add?pictureId=4&printSize=2&paperType=1&quantity=1 Request Method:GET Status Code:301 MOVED PERMANENTLY
- add /cartManager/add?:
- 添加 /cartManager/add?:
Request URL:http://127.0.0.1:8000/cartManager/add/?pictureId=4&printSize=2&paperType=1&quantity=1 Request Method:GET Status Code:201 CREATED
Request URL:http://127.0.0.1:8000/cartManager/add/?pictureId=4&printSize=2&paperType=1&quantity=1 Request Method:GET Status Code:201 CREATED
Request headers for both are pretty much the same, the only difference in request headers:
两者的请求标头几乎相同,唯一的区别是请求标头:
first is cartManager/add?pictureId=and so on and the second one is cartManager/add/?pictureId- the '/' after /add
第一个是cartManager/add?pictureId=等等,第二个是cartManager/add/?pictureId- /add 之后的'/'
Is there something wrong with my javascript?
我的javascript有问题吗?
回答by Alnitak
There's nothing wrong per-se, but you should add the trailing slash to /cartManager/add
yourself.
本身没有任何问题,但您应该为/cartManager/add
自己添加尾部斜杠。
What's happening is that the web server is sending a 301
redirect to the AJAX client with a new URL, so it issues a new request to the proper URL (i.e. with the trailing slash).
发生的事情是 Web 服务器正在301
使用新 URL 向 AJAX 客户端发送重定向,因此它向正确的 URL(即带有尾部斜杠)发出新请求。
回答by Capsule
This is happening because of this: http://httpd.apache.org/docs/2.0/mod/mod_dir.html#directoryslash
这是因为:http: //httpd.apache.org/docs/2.0/mod/mod_dir.html#directoryslash
Nohing to do with your javascript, this is pure Apache wizardry.
与您的 javascript 无关,这是纯粹的 Apache 魔法。
Of course, as pointed out in other answers, you should add a slash after "add" because "add" is obvisouly a folder, not a file.
当然,正如其他答案中所指出的,您应该在“添加”之后添加一个斜杠,因为“添加”显然是一个文件夹,而不是一个文件。
回答by Isaac Truett
Status Code:301 MOVED PERMANENTLY
No, your JavaScript is not causing this. It looks like your server is redirecting /cartManager/add
to /cartManager/add/
. Since the server wants a trailing slash, why not just add it and avoid the redirect?
不,您的 JavaScript 不会导致这种情况。看起来您的服务器正在重定向/cartManager/add
到/cartManager/add/
. 既然服务器想要一个尾部斜杠,为什么不添加它并避免重定向?
回答by Babak Naffas
The header has the clues you need.
标题包含您需要的线索。
Your request to '/cartManager/add' is being forwarded to '/cartManager/add/' (notice the ending forward slash).
您对“/cartManager/add”的请求被转发到“/cartManager/add/”(注意结尾的正斜杠)。
Replace your ajax call with
将您的 ajax 调用替换为
$.ajax({
url: '/cartManager/add/',
data:{
pictureId: currentImageId,
printSize: $("#size option:selected").val(),
paperType: $("#paperType option:selected").val(),
quantity: 1
},
success: function(){
$("#modal").html("<h1>ОК</h1><p>Closing in a sec</p>").delay(1000);
$("#modal").overlay().close();
}
});