$.ajax 发布请求进入 get

$.ajax post request going into get

本文关键字:get 请求 ajax 布请求      更新时间:2023-09-26

一直在敲打可能会到处走,以了解发生了什么。这是我的jquery ajax请求:

function newContact() {
        $.ajax({
                type: 'POST',
                contentType: 'application/json',
                // url: rootURL,
                url: "http://xxxxxxxxx.xxxxx/api/det",
                dataType: "json",
                //data: formToJSON(),
                data: '{"name":"tom","last":"test","email":"test@west.coast.com","password":"xyz"}',
                success: function(data, textStatus, jqXHR){
                        alert('Welcome');
                },
                error: function(jqXHR, textStatus, errorThrown){
                        console.log(' Error: ' + errorThrown);
                }
        });
}

现在,我已经用 curl 测试了 url 并且它可以工作,我已经检查了输入的 val() 是否被填充,它确实如此,我真的不明白为什么当我触发函数时我会收到一个 GET 请求,如下所示:

[11:08:47.607] GET http://xxxxx.com/?name=&last=&email=test%40email.test.com&passwrd=asdf&passwrd2= [HTTP/1.1 304 Not Modified 40ms]

这是发送到服务器的内容。现在,如果我打开Firebug的控制台,我会看到以下内容:

POST http://xxxxxxxxxxx.com/api/det

jquery.min.js (line 6)
HeadersPost
Request Headers
Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length  81
Content-Type    application/json; charset=UTF-8
Host    xxxxxxx.com
Referer http://xxxxxxxxx.com/
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
X-Requested-With    XMLHttpRequest

我真的无法理解。 当 Apache 收到 GET 请求时,上面提到的这个帖子是什么?

此外,我对可能是什么问题或如何开始故障排除一无所知,作为一个完全使用 ajax/json 的新手。

任何可以在这里提供一些启发的想法将不胜感激。

编辑现在多亏了这一点,我已经能够向前迈出一小步:将方法 post 添加到表单后,我已经能够发出 post 请求,但是它会转到/而不是我期望的位置(url:"http://xxxxxxxxx.xxxxx/api/det")。

解决方案是使用以下方法event.preventDefault()停止提交表单:

$("#subm").button({icons: {primary: "ui-icon-person"}}).click(function(event) {
  event.preventDefault();
  newContact();
});

您正在调用的 api (http://xxxxxxxxx.xxxxx/api/det) 中的方法是 Get 类型,并且您以"POST"类型调用可能是这种情况。

检查 API 中的方法类型。