>  Q&A  >  본문

javascript - ajax 获取txt文件,出错

使用ajax获取txt文件,出错,查了很多资料,没有找到原因。

直接通过浏览器访问,没有错误,
使用ajax却出错。。。

    $.get('http://api.91orange.cn/selectseat/603/arealist.txt', function(data) {
                //alert(data.success);
                alert("OK");
            }).fail(function(data) {
                //alert(data);
                var is_json;
                var json;
                try {
                    var json = $.parseJSON(data.responseText);
                    is_json = true;
                } catch (e) {
                    is_json = false;
                }

                if (is_json) {
                    // add_json_error(data, json);
                    alert('hson');
                    alert(data.message);

                } else {
                    // Show the response text as plaintext.
                    alert("data.status is "+data.status);
                    var status = data.status;
                    var statusText = data.statusText;

                    // If we've hit a 400 (Bad Request), show the responseText.
                    if (status === 400) {
                        alert('400');
                        statusText += ": " + data.responseText;
                    }
                    alert(status + " --- " + statusText);
                }
            }).always(function() {
                btn_sub.removeClass('disabled');
            });
$.ajax({
            type: 'GET',
            url: "http://api.91orange.cn/selectseat/603/arealist.txt",
            async: false,
            //dataType: "text",
            success: function (data){
                alert(data);
            },
            error: function(data, status, err){
                alert(status);
                alert(data);

            } 
        });

使用上面两种方法都出现错误,无语了,求解。。。

PHP中文网PHP中文网2748일 전466

모든 응답(1)나는 대답할 것이다

  • PHP中文网

    PHP中文网2017-04-10 13:15:05

    有评论说了,是 跨域 问题。

    关于跨域(Cross-Domain),推荐看这一篇:
    JavaScript跨域总结与解决办法

    讲得很详细,并提出了解决方案。注意其中的两句:

    1. 如果是 协议和端口 造成的跨域问题,“前台”是无能为力的;
    2. 在跨域问题上,域仅仅是通过 “URL的首部” 来识别而不会去尝试判断相同的ip地址对应着两个域或两个域是否在同一个ip上。(“URL的首部”指window.location.protocol +window.location.host,也可以理解为“Domains, protocols and ports must match”。)

    회신하다
    0
  • 취소회신하다