dqgg25493 2015-01-04 17:09
浏览 281

CORS在Firefox上被阻止

I want to use jQuery to run an asynchronous post with data to "[domain1]/first.php" on server #1 when a user on a page "[domain2]/unrelated.php" on server #2 clicks a link to "[domain2]/second.php?var1=data1?var2=data2" that's on server #2. I don't want to wait for "[domain1]/first.php" to finish or return back any response whatsoever.

  1. "[domain1]/first.php" is set up to process data (it also does a cURL to "[domain2]/unrelated2.php" on server #1) and not echo anything, and to finish with an "exit;" or "?>" line.
  2. "[domain2]/second.php" processes the data and echoes a page.

    • It works in Chrome, Opera and Firefox when it's synchronous. (but it's slow because of data processing, I want to avoid the wait after posting and before it follows the clicked link)
    • It works in Chrome or Opera when it's asynchronous.
    • Firefox gives a CORS error in the console for "[domain1]/first.php" when it's asynchronous:

"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at [domain1]/first.php. This can be fixed by moving the resource to the same domain or enabling CORS."


I've tried adding this to the top of "[domain1]/first.php":

// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    // cache for 1 day
}

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

    //exit(0);
}

The jQuery AJAX call on "[domain2]/unrelated.php" is:

    $.ajax({
        url: '[domain1]/first.php',
        type: 'POST',
        data: parameters
    });

All three browsers used for testing are up to date. The jQuery in use is 1.7.2.


I could detect Firefox and only make it use a synchronous post, but it hardly seems satisfying considering it works asynchronously in other two browsers.

Any thoughts or suggestions?

I could post how the request and response headers look like, but I'm unable to do so right now, I'll edit them in at a later time, I apologize. From searching around, this seems to primarily be a problem of the header that server #2 sends back in response from the original post from server #1, so I assume posting those headers is critical in solving this case? Maybe I have to uncomment the commented "//exit(0);"?

Thanks for your time!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求帮我调试一下freefem代码
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图