-1

I try to send an AJAX GET request from a sub domain to a main domain. Below is my AJAX code.

$.get('http://my-yii2-website.com/controller/test-method', 
      {data: '1')}, 
      function(returnedData){

        alert(returnedData);

      }, 
      'json'
);

This above code resides in the sub domain. In the main domain I have the controller and action method that looks like following.

public function behaviors()
{
    $subDomain = 'http://subdomain.my-yii2-website.com';

    return [
        'corsFilter' => [
            'class' => \yii\filters\Cors::className(),
            'cors'  => [
                'Origin' => [$subDomain, ],
                'Access-Control-Request-Method' => ['GET', ],
                'Access-Control-Request-Headers' => ['*'],
                'Access-Control-Allow-Credentials' => null,
                'Access-Control-Max-Age' => 3600,
            ],
        ],
    ];
}

public function actionTestMethod()
{
    $request = Yii::$app->request;
    if($request->isAjax)
    {
          return json_encode('OK!');
    }
    else
    {
          return json_encode('ERROR');
    }
}

It does work. But due to the if-statement in the actionTestMethod(), it does not see the request as an AJAX request. It returns the predefined 'ERROR' message. I am working with Yii framework 2.0.

O Connor
  • 4,236
  • 15
  • 50
  • 91
  • In your example your domains are different http://my-yii2-website/controller/actionMethod and http://subdomain.my-yii2-website.nl. Maybe you miss .nl in ajax url – Panoptik Jan 05 '15 at 10:14
  • Just edited my question. In my original code I have checked, the domain url and the sub domain url are correct. I just don't understand why it is a bad request while I did define the POST method properly. – O Connor Jan 05 '15 at 10:22
  • Next: actions array in cors branch is in wrong level. cors and actions branches should be siblings – Panoptik Jan 05 '15 at 10:48
  • You're trying to make a *post* request using `$.get`?! Give that and the error message you quote, it looks like you've shared the wrong piece of Ajax code. – Quentin Jan 05 '15 at 11:06
  • @Panoptik: I have edited my code with the GET method. This time the AJAX GET method can reach the actionTestMethod in the main domain. But due to the if-statement it returns the predefined 'ERROR' message because it does not see the request as an AJAX request. – O Connor Jan 05 '15 at 11:08
  • @Quentin: At the beginning I have tried to use the POST method with $.ajax(type: 'POST'); but it didn't work. So I then try with the GET method and have changed my code sample here into GET, but forget to change the question title. Why did I edit my question with the GET method? because It works with the GET method. But the next issue is that it does not see my request as an AJAX request. – O Connor Jan 05 '15 at 11:13

1 Answers1

0

isAjax tests for the presence of an X-Requested-With header on the assumption that anyone making an Ajax request will set that (non-standard) header.

jQuery will set it by default for same origin requests. It will not set it for cross origin requests (because it is non-standard, normal CORS settings will reject it).

Either set it explicitly or use some other method to determine if the request is ajax or not.

(Note that, in general, "is ajax" is a bad test, "would like JSON instead of HTML" is normally a better test and you can key that off the Accept header)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • So how can I check if it is a AJAX request or not using any Yii 2.0 method? because otherwise I want the application to stop executing if it is not a AJAX request. – O Connor Jan 05 '15 at 12:18
  • @OConnor — As I said, set the `X-Requested-With` header explicitly. – Quentin Jan 05 '15 at 12:19