Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Cross Domain in Codeigniter

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?

like image 288
Jignesh.Raj Avatar asked Dec 26 '22 16:12

Jignesh.Raj


2 Answers

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) {
        }
    }
like image 65
Jignesh.Raj Avatar answered Jan 10 '23 08:01

Jignesh.Raj


move

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

to the top

like image 40
MACMAN Avatar answered Jan 10 '23 10:01

MACMAN