【问题标题】:jQuery $.get not working (Not Found 404)jQuery $.get 不工作(未找到 404)
【发布时间】:2015-07-26 14:09:18
【问题描述】:

请帮我解决一个问题 - get 函数不适用于 http://wtfismyip.com/texthttp://www.passwordrandom.com/query?command=ip

代码:

<span id="global_ip" class="global_ip">global ip - get test</span>
<script type="text/javascript">
$('#global_ip').click(function () {  
    alert('click!');      
    $.get({
        type: "GET",
        url: "http://www.passwordrandom.com/query?command=ip",
        dataType: "text"
    }).done(function (res) {
        alert(res);
    }).fail(function (jqXHR, textStatus, errorThrown) {
        alert("Call failed: " + textStatus + ", " + errorThrown);
    });
});

【问题讨论】:

标签: javascript jquery ajax get ip


【解决方案1】:

你不能这样做,这违反了同源政策。

由于浏览器安全限制,大多数“Ajax”请求都受same origin policy的约束;请求无法从不同的域、子域、端口或协议成功检索数据。

但这可以帮助你实现你想要的:Get ip address with javascript

【讨论】:

    【解决方案2】:

    试试这个:

        <span id="global_ip" class="global_ip">global ip - get test</span>
            <script type="text/javascript">
                    $('#global_ip').click(function()
                    {
                            $.get("http://www.passwordrandom.com/query?command=ip", function(data)
                            {
                                    alert(data);
                            });
                    });
    </script>
    

    【讨论】:

    • 你的API在同一个域下吗?你得到什么错误?
    猜你喜欢
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2022-12-04
    • 1970-01-01
    • 2014-01-16
    • 2017-01-17
    • 2018-08-01
    相关资源
    最近更新 更多