JQuery的阿贾克斯发送GET而不是POST [英] JQuery Ajax is sending GET instead of POST

查看:95
本文介绍了JQuery的阿贾克斯发送GET而不是POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code触发得到一个POST HTTP请求来代替。

The following code triggers a GET instead of a POST HTTP request.

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 ?

我在谷歌浏览器看到检查和Firefox检查浏览器发送GET。下面是从浏览器:

I see in Google Chrome Inspect and Firefox Inspect that the browser sends a GET. Here is from Chrome:

请求的URL:http://本地主机/样品 - codeS / UPDATE%20MYSQL / API /添加/   请求方法:GET状态code:200 OK

Request URL:http://localhost/SAMPLE-CODES/UPDATE%20MYSQL/api/add/ Request Method:GET Status Code:200 OK

解决

所谓的URL./api/add是真正张贴到./api/add/index.php。原来,打电话'./api/add<$c$c>/index.php '或'./api/add<$c$c> / 给我一个POST请求。

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.

这只是一个错误的URL,但由于某些原因,我得到一个成功的GET请求'.API /加/'.

It was just a wrong URL, but for some reason I was getting a successful GET request to '.api/add/'.

推荐答案

在MVC的一些问题。出于某种原因,当我删除[HttPost]它能正常工作,即使我告诉AJAX使用POST。

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 .

  • 原来,你需要使用

键入:POST

  • 尽管jQuery的页面的例子说,使用

方法:POST

现在是POST的

不过挖的文档中后,我发现这一点。

But after digging in the documentation I found this.

这篇关于JQuery的阿贾克斯发送GET而不是POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆