0

i will try to a bulk data send to another domain via api that will created at controller in laravel project

my function

 public function test_prod_insert()
    {
       $usrl= url('/public/wheel_images/rohana_imgs');
        $users = DB::table('products')->select('id','title','sku','image1')->skip(0)->take(2)->get();
        echo"<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>
        <script type='text/javascript' src='https://trk.mtrl.me/tracking.js?token=**********'></script>";

        foreach($users as $data){
    echo"<script>
        var model = {
  'time': 1518004715732,
  'token': '***************',
  'platform': 'laravel',
  'pluginVersion': '1.1.0',
  'params': {
    'categories': [
      '2'
    ],
    'id': '<?= $data->id ?>',
    'sku': '<?= $data->sku ?>',
    'imageUrl': '<?= $usrl ?>/<?= $data->image1 ?>',
    'name': '<?= $data->title ?>',
    'price': '',
    'url': '',
    'options': [

    ]
  }
};

$.ajax({
    type: 'POST',
    data: JSON.stringify(model),
    url: 'https://trk.mtrl.me/product',
    contentType: 'application/json'
}).done(function(res) {       
    console.log('res', res);
    // Do something with the result :)
});
</script>";
}
    }

in console show " mysite has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response " i try some similar answer but the issue is not solved please help me to solve this issue...

Andrew
  • 840
  • 3
  • 17
  • 43

1 Answers1

1

There is nothing that you can do much in CORS: But it's all realted to the host site that you are calling, Basically it's a browser feature so it will prevent the request to host site if in the headers it doesn't see

"Access-Control-Allow-Origin: *"

So in your host site which you are calling, just make sure that it accept connections from your domain and then you can call the API.

EDIT Writing this again don't make sense, But this is how you can implement it

Add CORS as Middleware in Laravel

BlackXero
  • 880
  • 4
  • 17
  • "Access-Control-Allow-Origin: *" how i apply this in script – Andrew Jan 13 '20 at 10:39
  • because postman makes curl request in the back. but XHR request are browser based. what's your backend programming language – BlackXero Jan 13 '20 at 10:44
  • laravel is used – Andrew Jan 13 '20 at 10:54
  • I am editing my answer you can read, how you can control this – BlackXero Jan 13 '20 at 10:57
  • ya i follow the same steps but show Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://trk.mtrl.me/product. (Reason: missing token ‘content-type’ in CORS header ‘Access-Control-Allow-Headers’ from CORS preflight channel). – Andrew Jan 13 '20 at 11:04
  • This is a bad practice but put this line in public/index.php file header("Access-Control-Allow-Origin: *"); and then try again – BlackXero Jan 13 '20 at 11:06
  • i will try in admin side – Andrew Jan 13 '20 at 11:36