【问题标题】:jQuery POST request from inside an iFrame times out来自 iFrame 内部的 jQuery POST 请求超时
【发布时间】:2013-01-07 16:15:01
【问题描述】:

我有一个 iframe,它通过小书签注入到任意网页中。它指向http://localhost:5000/test,这是一个运行以下Javascript的简单HTML页面:

$.ajax({
  type: "POST",
  url: "http://localhost:5000/parse",
  data: {data:"hello world"}
}).done(function( msg ) {
  console.log("Success!!", arguments);
}).fail(function(jqXHR, textStatus, errorThrown) {
  console.log("Error", arguments);
});

我可以在网络检查器中看到调用已发出,但它一直处于 (pending) 状态,直到请求在 30 秒后超时。服务器日志显示服务器根本没有受到攻击。奇怪的是,如果我删除了 ajax 请求中的 data 参数,服务器就会受到攻击,一切都会按预期工作。

$.ajax({
  type: "POST",
  url: "http://localhost:5000/parse"
}).done(function( msg ) {
  console.log("Success!!", arguments); // this works.
});

我不认为跨域策略问题会成为问题,因为 iframe 和 ajax 请求目标位于同一个域中。我在这里想念什么?浏览器级别是否存在阻止此请求通过的内容?

【问题讨论】:

    标签: jquery html ajax iframe bookmarklet


    【解决方案1】:

    尝试将 dataType 和 contentType 添加到 ajax 设置中

    $.ajax({
       type: "POST",
       url: "http://localhost:5000/parse",
       contentType: 'application/json; charset=utf-8',
       dataType: 'json',
       data: {data:"hello world"}
     }).done(function( msg ) {
         console.log("Success!!", arguments);
     }).fail(function(jqXHR, textStatus, errorThrown) {
         console.log("Error", arguments);
     });
    

    【讨论】:

    • 虽然添加这些对我来说可能是个好主意(我现在有了),但它并没有解决问题。在超时之前,该请求仍处于待处理状态。
    • 你可能需要对数据进行字符串化,数据:JSON.stringify({data:"test"}),这里建议:stackoverflow.com/questions/2845459/…
    • 不幸的是,用 JSON.stringify 序列化对象没有效果。
    猜你喜欢
    • 2011-11-30
    • 2016-11-02
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 2012-04-08
    • 2017-04-01
    • 2016-03-19
    相关资源
    最近更新 更多