Ionic can't get open cors

后端 未结 3 1618
慢半拍i
慢半拍i 2021-01-17 03:34

I am trying to get API data from live server in ionic android app but it returns this error:

Access to XMLHttpRequest at \'https://example.com/api/categories         


        
相关标签:
3条回答
  • 2021-01-17 04:07

    SOLVED

    Thanks to Stephen Romero for pointing the important part of this solution,

    based on stephen answer I added this code to my function:

    const httpOptions = {
          headers: new HttpHeaders({
            'Accept': 'application/json, text/plain',
            'Content-Type': 'application/json'
          })
        };
    

    and used it in my get request like:

    return this.http.get(`${this.apiUrl}`, httpOptions).pipe(
    

    Now the for header permissions I used (installed) this package for on my laravel app and made config file set as code below:

    return [
        /*
        |--------------------------------------------------------------------------
        | Laravel CORS
        |--------------------------------------------------------------------------
        |
        | allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
        | to accept any value.
        |
        */
    
        'supportsCredentials' => false,
        'allowedOrigins' => ['*'],
        'allowedOriginsPatterns' => [],
        'allowedHeaders' => ['*'],
        'allowedMethods' => ['GET', 'OPTIONS'],
        'exposedHeaders' => [],
        'maxAge' => 0,
    
    ];
    

    FOR those who doesn't use Laravel

    Set your headers like this:

    if($request_method = 'GET'){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, OPTIONS');
        header('Access-Control-Allow-Headers: Authorization, Expires, Pragma, DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range');
        header("Access-Control-Expose-Headers: *");
    }
    

    The most important part of this headers is Access-Control-Allow-Headers part, if you simply use * it won't work! you need to set headers name.

    Hope it helps.

    Update

    Forgot to mention in order to avoid error 301 you need to remove / from end of your api url.

    // my api (before)
    https://example.com/api/categories/
    
    //changed to
    https://example.com/api/categories
    
    0 讨论(0)
  • 2021-01-17 04:26

    I solved my issue using these Headers for my API:

    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Credentials: true ");
    header("Access-Control-Allow-Methods:GET,POST");
    header("Access-Control-Allow-Headers: Authorization, Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control");

    And Angular Http:

    //GET data details
     getData(authToken){
          const httpOptions = {
              headers: new HttpHeaders({
                  'Accept': 'application/json, text/plain',
                  'Content-Type':  'application/json',
                  'Authorization': authToken
                })
       };
       //console.log(authToken);
       return this.http.get(this.apiGetUrl, httpOptions).retry(3);
     }
    Like the previous answer, an Options request automatically gets sent with the GET or POST. If you have apache servers, you can echo$headers = apache_request_headers(); to see what is all coming through. Comparison for $_SERVER and Apache here.

    In my case, I run if statements:

    if(isset($headers["Authorization"]) && isset($headers["Content-Type"])){
      //handle get request
      }
    else{
      //handle options request
       echo " False,Re-routing Options Request";
      }

    I would test your HTTP call in the browser and look at dev tools to confirm the requests being sent. I hope this helps!

    0 讨论(0)
  • 2021-01-17 04:32

    Perhaps at some point a preflight OPTIONS request is done by the client and since it isn't a listed method in your Access-Control-Allow-Methods it ends up in a CORS issue.

    You should try to make a request to your server endpoint with OPTIONS method to check if this is the case, you can use POSTMAN to make this test.

    Then try to add the OPTIONS method to the Access-Control-Allow-Methods and check the difference.

    0 讨论(0)
提交回复
热议问题