Set Cross Domain in Codeigniter

痞子三分冷 提交于 2019-12-09 03:21:35

问题


When ever I run hosted project service run perfectly..

when i test with Other project that give me error or i could not getting response from Services. i try a lot but not working

my Ajax Call:

self.ValidLogin = function () {
         try {
            $.ajax({
                type: "GET",
                url: "http://xxx.xxx.xxx.xxx/TEST/index.php/TestController/TestMethod?UserName=superadmin&Password=super",
                ,
                crossDomain: true,
                contentType: "application/json; charset=utf-8",
                async: false,
                dataType: 'json',
                cache: false,
                success: function (response) {
                    alert("valid response");
                },
                error: function (ErrorResponse) {
                    alert("error");
                }

            });
        }
        catch (error) {
            alert("Catch:" + error);
        }
    }

Service Side:

public function TestMethod()
    {
        parse_str($_SERVER['QUERY_STRING'],$_GET);
        $UserName = $_GET['UserName'];
        $Password = $_GET['Password'];

        $this->load->model('LoginModel');
        $result = $this->LoginModel->Login($UserName,$Password);

        header('Content-type: application/json');
        header('Access-Control-Allow-Origin: *');
        echo json_encode($result);

    }

what should i do?


回答1:


After Long Rnd Got Solution

self.ValidLogin= function () {
        try {
            $.ajax({
                type: "GET",
                url: "http://xxx.xxx.xxx.xxx/TEST/index.php/TestController/TestMethod?UserName=superadmin&Password=super",
                crossDomain: true,
                contentType: "application/x-www-form-urlencoded",
                async: false,
                dataType: 'json',
                processData: false,
                cache: false,
                success: function (response) {
                    alert("valid response");
                },
                error: function (ErrorResponse) {
                    alert("error");
                }
            });
        }
        catch (error) {
        }
    }



回答2:


move

header('Access-Control-Allow-Origin:*');

to the top




回答3:


I have try this on my controller:

function __construct() {
        parent::__construct();
        $this->output->set_header('Access-Control-Allow-Origin: *');
}

And it work! But no for all the pages. I still looking for a solution for all content. I guess this config should be on autoload or something.



来源:https://stackoverflow.com/questions/20511988/set-cross-domain-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!