【问题标题】:jQuery ajax .done not calling with Sinatra JSON responsejQuery ajax .done 不使用 Sinatra JSON 响应调用
【发布时间】:2013-11-29 12:30:55
【问题描述】:

我正在使用一个简单的 Sinatra 项目重新开始工作,但是在执行 jQuery .done() 块时遇到了一个小问题。

背景: - 本地开发机器(目前) - Mac OS X 小牛 - Apache/mysql/PHP 堆栈在 80、8888 上运行和监听 - sinatra 在 3000 上运行

Sinatra 中的处理程序已被剥离到最低限度:

post '/' do 
    status 200
    content_type :json
    params.to_json
end 

提交的表单:

<form action="http://localhost:3000/ph/api" method="post" id="apitest">
    <input type="hidden" name="recipes[]" value="cornbread"/>
    <input type="hidden" name="recipes[]" value="taters"/>
    <input type="hidden" name="recipes[]" value="PO-TAY-TOES"/>
    <input type="submit"/>
</form>

带有 jQ​​uery 的 AJAX(1.10,google 托管):

$('#apitest').submit(function(event) {
    event.preventDefault();
    var myreq = $.ajax({
        type:"POST",
        url:"http://localhost:3000/",
        dataType:"text",
        data:$('#apitest').serialize()
    });
    myreq.done(function() {
        alert("done");
    });
    myreq.always(function() {
        alert("always");
    });
});

当我点击时,表单提交。看看查尔斯的交通,一切看起来都是合法的。

这是请求:

POST / HTTP/1.1
Host: localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:22.0) Gecko/20100101 Firefox/22.0
Accept: text/plain, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost/apitest.html
Content-Length: 70
Origin: http://localhost
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

recipes%5B%5D=cornbread&recipes%5B%5D=taters&recipes%5B%5D=PO-TAY-TOES

回复如下:

HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
Content-Length: 48
X-Content-Type-Options: nosniff
Connection: keep-alive
Server: thin 1.6.1 codename Death Proof

{"recipes":["cornbread","taters","PO-TAY-TOES"]}

最后,我的“always”处理程序触发了。 “完成”似乎永远不会触发。我希望这里有一些与安全有关的东西(相同的起源?)这让我望而却步,但我研究这个问题的时间已经够长了,以至于我被炒了或错过了一些非常明显的东西。

非常感谢您提供的任何见解或提示!

一个fail()块添加如下:

myreq.fail(function(xhr, status, err) {
    console.log(xhr.status + " - " + xhr.statusText);
});

控制台输出:

0 - error

Status 和 err 分别给出什么和“错误”。

【问题讨论】:

  • 尝试添加一个失败处理程序,看看它是否会触发,以及出现什么错误。
  • Fail 给我一个状态 0 和一条“错误”消息,并且查看对象的 responseText 也是“”。
  • 最新版本的 jQuery 还是会失败吗?
  • 使用 jQuery 1.10.2。不能使用 2.x,因为很遗憾需要 IE8 支持。

标签: jquery ruby ajax json sinatra


【解决方案1】:

如果你使用的是 json

var myreq = $.ajax({
    type:"POST",
    url:"http://localhost:3000/",
    dataType:"json", 
    data:{"vl":$('#apitest').serialize()}
});

myreq.done(function (response, textStatus, jqXHR){
   alert("done");
});

// callback handler that will be called on failure
myreq.fail(function (jqXHR, textStatus, errorThrown){
     alert("errors"+textStatus);
});

myreq.always(function() {
    alert("always");
}); 



provided http://localhost:3000/ should return a valid json back. 

【讨论】:

    【解决方案2】:

    归根结底,通过直接 AJAX 处理这个是一团糟 - 从来没有弄清楚发生了什么。在服务器上使用粗略的中介使一切顺利。

    【讨论】:

      猜你喜欢
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2014-10-24
      • 1970-01-01
      • 2015-10-01
      • 2013-10-31
      相关资源
      最近更新 更多