JQuery Ajax 正在发送 GET 而不是 POST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12195883/
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 is sending GET instead of POST
提问by Timothée HENRY
The following code triggers a GET instead of a POST HTTP request.
以下代码触发 GET 而不是 POST HTTP 请求。
function AddToDatabase() {
this.url = './api/add';
}
AddToDatabase.prototype.postData = function(dataToPost) {
$.ajax({
type: "POST",
url: this.url,
data: dataToPost,
context: this,
success: this.onSuccess
});
};
var AddToDatabase = new AddToDatabase();
data = {data: 'coucou'};
AddToDatabase.postData(data);
Why, and how can I get a POST ?
为什么,以及如何获得 POST ?
I see in Google Chrome Inspect and Firefox Inspect that the browser sends a GET. Here is from Chrome:
我在 Google Chrome Inspect 和 Firefox Inspect 中看到浏览器发送了一个 GET。这是来自Chrome:
Request URL:http://localhost/SAMPLE-CODES/UPDATE%20MYSQL/api/add/ Request Method:GET Status Code:200 OK
请求地址:http://localhost/SAMPLE-CODES/UPDATE%20MYSQL/api/add/ 请求方法:GET 状态码:200 OK
SOLVED
解决了
The URL called './api/add' was to actually post to './api/add/index.php'. Turns out that calling './api/add/index.php
' or './api/add/
' gives me a POST request.
名为“./api/add”的 URL 实际上是发布到“./api/add/index.php”。原来调用 './api/add /index.php
' 或 './api/add /
' 给了我一个 POST 请求。
It was just a wrong URL, but for some reason I was getting a successful GET request to '.api/add/'.
这只是一个错误的 URL,但由于某种原因,我成功地收到了对“.api/add/”的 GET 请求。
回答by Piotr Kula
Some problem on MVC. For some reason when I remove the [HttPost] it works as expected, even though I am telling ajax to use POST .
MVC 上的一些问题。出于某种原因,当我删除 [HttPost] 时,它按预期工作,即使我告诉 ajax 使用 POST 。
- It turns out you need to use
- 事实证明你需要使用
type: "POST"
类型:“POST”
- Even though the example on jQuery page says to use
- 即使 jQuery 页面上的示例说要使用
method : "POST"
方法:“POST”
Now it POST's
现在是 POST 的
But after digging in the documentation I found this.
但是在深入研究文档后,我发现了这一点。
回答by texelate
I had this issue and per @FAngle's suggestion it was because my .htaccess was removing trailing slashes — and I had set the url to /ajax/foo/bar/
and not/ajax/foo/bar
. The redirect changes the request from POST to GET. Remove the / and problem solved!
我遇到了这个问题,根据@FAngle 的建议,这是因为我的 .htaccess 正在删除尾部斜杠 - 我已将 url 设置为/ajax/foo/bar/
和 not /ajax/foo/bar
。重定向将请求从 POST 更改为 GET。删除 / 并解决问题!
回答by Jossef Harush
The URL './api/add'
actually got redirected to './api/add/index.php'
. therefore this bizarre side-affect which the new request after the redirect sent using GET
instead of POST
该 URL'./api/add'
实际上被重定向到'./api/add/index.php'
. 因此,这种奇怪的副作用是重定向后发送的新请求使用GET
而不是POST
Solution
解决方案
- use the full url
'./api/add/index.php'
- or add a slash
'./api/add/'
.
- 使用完整网址
'./api/add/index.php'
- 或添加斜杠
'./api/add/'
。
回答by emragins
I noticed this behavior as well where my POST was sending a GET. The scenario's pretty unique, but maybe it will help someone.
我在 POST 发送 GET 的地方也注意到了这种行为。这个场景非常独特,但也许它会对某人有所帮助。
This was happening to me on my User Role-edit page, where I was using ajax (post) as an immediate action when a role was checked or unchecked.
这是在我的用户角色编辑页面上发生在我身上,其中我在检查或未选中时使用Ajax(post)作为立即动作。
I also had the server configured to re-authenticate the user (and redirect them) whenever their role information changed so that their claims would be refreshed.
我还将服务器配置为在用户角色信息发生更改时重新验证用户(并重定向他们),以便刷新他们的声明。
The brutal cycle ended up as:
残酷的循环最终变成了:
First Role Update -- POST -- 200 success
Next Role Update -- POST -- 302 Found -> Redirect (I did not notice this until I used Fiddler rather than Chrome's network monitor)
Redirect Call from (2) (Same URL) -- GET -- 404 Not Found (since I only allowed Post)
GOTO (1)
第一次角色更新 -- POST -- 200 成功
Next Role Update -- POST -- 302 Found -> Redirect (我没有注意到这一点,直到我使用 Fiddler 而不是 Chrome 的网络监视器)
从 (2) (Same URL) 重定向调用 -- GET -- 404 Not Found(因为我只允许 Post)
转到 (1)
I ended up changing the server to bypass re-authentication/claims update when it detected an ajax request (based on the Accept Types).
当检测到 ajax 请求(基于接受类型)时,我最终更改了服务器以绕过重新身份验证/声明更新。
回答by tonejac
I found that when using dataType: 'jsonp'
it converts the request to a GET
. I changed it to dataType: 'json'
it changed from GET
to POST
.
我发现使用dataType: 'jsonp'
它时会将请求转换为GET
. 我把它改成它dataType: 'json'
从GET
变成POST
。
回答by Parham
I had a similar issue and it started working for me as soon as I removed the hardcoded https://
from my url.
我有一个类似的问题,一旦我https://
从我的 url 中删除了硬编码,它就开始为我工作。
jQuery.ajax({
type: "POST",
url: "www.someurl.com",//instead of "https://www.someurl.com"
data: { foo:"bar"},
success: function(d){ console.log(d); },
dataType: "JSONP"
});
回答by Mr_DeLeTeD
For me, your piece of code look OK, but if you want to be sure, you can use $.post instead of $.ajax
对我来说,你的代码看起来不错,但如果你想确定,你可以使用 $.post 而不是 $.ajax
$.post('ajax/test.html', function(data) {
$('.result').html(data);
});
jquery link : http://api.jquery.com/jQuery.post/
jquery 链接:http: //api.jquery.com/jQuery.post/
回答by Viktor S.
Check out your .htaccess file or search for some other thing that may redirect your request
查看您的 .htaccess 文件或搜索其他可能重定向您的请求的内容
回答by Farid Movsumov
I had the same problem and found this question but answers didn't solve my problem. I eventually solve it by removing contentType
field in ajax request.
我遇到了同样的问题并找到了这个问题,但答案并没有解决我的问题。我最终通过删除contentType
ajax 请求中的字段来解决它。
contentType: "application/json",
回答by Callan
I had this issue, and it turned out to be a URL Rewrite module in IIS.
我遇到了这个问题,结果是 IIS 中的 URL 重写模块。
I'm using ASP.NET MVC and WebAPI. I created rule to force lowercase URLs so social networks don't view the same URL as two different pages.
我正在使用 ASP.NET MVC 和 WebAPI。我创建了强制使用小写 URL 的规则,这样社交网络就不会像两个不同的页面一样查看相同的 URL。
For example:
例如:
"http://url.com/View/Something/123GuidIdSomething"
“ http://url.com/View/Something/123GuidIdSomething”
vs
对比
"http://url.com/view/something/123guididsomething"
“ http://url.com/view/something/123guididsomething”
This however, was somehow messing with my ajax requests. I disabled the rule and the problem was resolved.
然而,这在某种程度上弄乱了我的 ajax 请求。我禁用了规则,问题就解决了。