【问题标题】:Set Cross Domain in Codeigniter在 Codeigniter 中设置跨域
【发布时间】:2013-12-29 00:05:47
【问题描述】:

当我运行托管项目服务时运行完美..

当我使用其他项目进行测试时出现错误或我无法从服务获得响应。 我尝试了很多但没有工作

我的 Ajax 调用:

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);
        }
    }

服务端:

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);

    }

我该怎么办?

【问题讨论】:

  • 你检查你的get成功了吗?
  • 那总是在 alert('Error')
  • 你检查过这个question吗?
  • 你有任何控制台错误吗?

标签: php json codeigniter codeigniter-2


【解决方案1】:

Long Rnd 得到解决方案后

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】:

移动

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

到顶部

【讨论】:

  • 我试过但结果一样:(
【解决方案3】:

我已经在我的控制器上尝试过这个:

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

而且它有效! 但是所有页面都没有。我仍在寻找所有内容的解决方案。 我猜这个配置应该是自动加载什么的。

【讨论】:

    猜你喜欢
    • 2011-05-10
    • 1970-01-01
    • 2017-10-06
    • 2017-07-11
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2021-08-29
    相关资源
    最近更新 更多