【问题标题】:$.ajax post request going into get$.ajax 发布请求进入 get
【发布时间】:2013-09-16 13:03:44
【问题描述】:

为了了解正在发生的事情,可能会四处寻找。 这是我的 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 req 时,上面提到的这个 POST 是什么?

此外,作为一个 ajax/json 的新手,我不知道可能是什么问题或如何开始故障排除。

任何可以在这里阐明一些想法的想法将不胜感激。

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

【问题讨论】:

  • 函数是如何触发的?它在事件处理程序中吗?
  • 我认为same origin policy违规
  • 该函数由jquery点击函数触发: $("#subm").button({icons: {primary: "ui-icon-person"}}).click(function() { newContact(); });

标签: javascript jquery ajax json http-post


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你调用的api(http://xxxxxxxxx.xxxxx/api/det)中的Method是Get类型的,你调用的可能是“POST”类型的情况。

    检查 Api 中的方法类型。

    【讨论】:

    • 嗨,谢谢你的回答事实上不是,如果我向那个 url 发送一个 json post 请求,数据就会被很好地插入,作为证明。我用 curl 测试了它
    猜你喜欢
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 2015-07-24
    相关资源
    最近更新 更多