简体   繁体   English

JavaScript:“同源政策”失败

[英]JavaScript: “same-origin policy” failure

I've tried innumerable permutations of code and the nearest I've gotten to success is a warning about the "same-origin policy", or I'm bounced to the sign-in page for the application, which is written using CodeIgniter. 我已经尝试了无数种代码置换,而我成功尝试的最近一次是关于“同源政策”的警告,或者我跳回了使用CodeIgniter编写的应用程序登录页面。

For the sake of keeping this sensible, on domain-one.com, I'm using: 为了保持明智,我在domain-one.com上使用:

(function($) {
    var url = 'http://domain-two.com/get_data?callback=?';
    $.ajax({
        type: 'GET',
        url: url,
        async: false,
        contentType: "application/json",
        dataType: 'jsonp',
        success: function(json) {
            console.log(json);
        },
        error: function(e) {
            console.log(e.message);
        }
    });
})(jQuery);

In the PHP on domain-two.com, I'm using: 在domain-two.com上的PHP中,我正在使用:

public function get_data () {
    $results = $this->model->method();
    if (count(results)>0):
        foreach (results as $array):
            $array['index'] = $array['value'];
            $array[] = $array_result;
        endforeach;

        header('content-type: application/json; charset=utf-8');
        header("access-control-allow-origin: *");

        $json = json_encode($array);

        # JSON if no callback
        if( ! $this->input->get('callback')):
            exit( $json );
        endif;
        # JSONP if valid callback
        if(is_valid_callback($this->input->get('callback'))):
            exit( $this->input->get('callback') . "(" . $json . ")" );
        endif;

    else:
        return false;
    endif;
}

At this point, it's worth mentioning that when I visit the link for the PHP method, it gives me the data I need. 在这一点上,值得一提的是,当我访问PHP方法的链接时,它将为我提供所需的数据。 If, however, I do so via the web page containing the JavaScript, it does not ("same-origin policy" error, or I'm bounced to the sign-in page). 但是,如果我通过包含JavaScript的网页进行操作,则不会(“同源策略”错误,或者被退回登录页面)。

Help regarding how I make use of a callback is, at best, scant (some advise using an actual callback function, others claim no such function is required). 关于我如何使用回调的帮助充其量是很少的(有些建议使用实际的回调函数,而另一些声称不需要此类函数)。

After 5 days at this, I have no idea what I'm meant to be doing, given the sheer number of different techniques, none of which appear to work for me. 经过五天的时间,鉴于大量不同的技术,我不知道我要做什么,但这些对我来说似乎都不起作用。

Any advice would be much welcome! 任何建议将非常欢迎!

In your php on domain-two.com 在您的php上domain-two.com

header('Access-Control-Allow-Origin: http://domain-one.com');
header('Access-Control-Allow-Origin: http://domain-two.com');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM