【问题标题】:jQuery Ajax problems on Firefox and Chrome, Opera (works well on Safari)Firefox 和 Chrome、Opera 上的 jQuery Ajax 问题(在 Safari 上运行良好)
【发布时间】:2011-12-23 10:54:40
【问题描述】:

我有一小段带有 jQ​​uery 的 HTML 代码。在我的代码中,我调用了一个简单的 ajax 调用。它在 Safari 上运行良好,但在 Firefox 和 Chrome 上运行良好。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>untitled</title>
    <meta name="generator" content="TextMate http://macromates.com/">
    <meta name="author" content="Daniel">
    <!-- Date: 2011-11-07 -->

    <script type='text/javascript' src='http://media.smashingmagazine.com/themes/smashing/js/jquery-1.5.2.min.js?ver=3.1.3'></script>
    <script type='text/javascript' src='http://media.smashingmagazine.com/themes/smashing/js/jquery-ui-1.8.2-min.js?ver=3.1.3'></script>
</head>

<script type="text/javascript">
    var jwt = "";
    var certUrl = "";

    function getJwt() {
        $.get(
            "http://signerapp.appspot.com/signerapp",
            function(data) { jwt = data;}
        );
    }
    function checkAvailable() {
        var postObject = {};
        postObject.certUrl = certUrl;
        postObject.jwt = jwt;
        postObject.action = "available";
        postObject.shortUrl = $('#shortUrl').attr('value');
        $.ajax( {
                    url: "http://clickin-shorturl.appspot.com/urlshortener/v1/url",
                    type: "POST",
                    data: JSON.stringify(postObject),
                    success: function(data) { alert(data); }
                });
    }
    $(document).ready(function() {
        getJwt();
    });
</script>
<body>
</br>
</br>
</br>
</br>
</br>
<h4>This is sample for creating url shortener</h4>
<p>Check available:</p>
<div>
    <!-- Check availability-->
<table border="1" width="540" height="100%">
    <!-- Check availability-->
    <tr>
        <td>
            <form>
                Action: <input type="textbox" id="action1" name="action1" value="available" disabled="disabled" size="20"></br>
                Short URL: <input type="textbox" id="shortUrl" name="shortUrl" value="aaaa" size="20"></br>
                <input type="button" id="checkButton" name="checkButton" value="Check Available" onClick="checkAvailable();"></br>
            </form>
        </td>
    </tr>
    <tr>
</table>

</div>

</body>
</html>

问题在于调用上面的 $.get 和 $.ajax。 使用 Safari,我得到了准确的结果:$.get 处的 base64 字符串和 $.ajax 处的 json 但是使用 Firefox 我什么都收不到 虽然响应代码是“200 OK”,但正文确实包含任何内容。

【问题讨论】:

    标签: jquery ajax firefox google-chrome browser


    【解决方案1】:

    这是因为same origine policy。您无法使用 ajax 请求加载其他网站内容。如果你想这样做,你必须为此使用 JSONP。或者您可以为此使用服务器端代理。

    【讨论】:

      【解决方案2】:

      您可以使用 firefox 的 firebug 或 chrome 的 inspect 元素来查找有关错误的更多信息

      【讨论】:

        猜你喜欢
        • 2018-09-25
        • 2014-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多