php - CORS request in AngularJs -


i have created mail service on php, service send letter user. here code:

<?php require 'vendor/autoload.php'; header('content-type : application/json'); header('access-control-allow-origin: *');  header('access-control-allow-methods: post, get, put, delete, options'); header('access-control-allow-headers: x-requested-with, content-type');  $response = array(       'text' => ''); if(!empty($_post)){ $json = json_decode($_post["req"]); $key     = $json->key; $sub     = $json->subject; $toemail = $json->emailto; $fromemail   = $json->emailfrom; $html    = $json ->html;  $sendgrid = new sendgrid($key); $email    = new sendgrid\email(); $email->addto($toemail)       ->setfrom($fromemail)       ->setsubject($sub)       ->sethtml($html);  $sendgrid->send($email);  $response['text'] = 'email sent '.$fromemail.' to'. $toemail.'. success.'; }else{ $response['text'] = 'sorry! $_post undefind'; } echo json_encode($response);  ?>  

i need create cross domain request service using angular.js. here code:

app.config(['$httpprovider', function ($httpprovider) {     $httpprovider.defaults.usexdomain = true;     delete $httpprovider.defaults.headers.common['x-requested-with']; } ]);  app.controller("emailctrl", function ($scope, $http) {      var dataforadminnewsletter = angular.tojson({         key: "********************************************",         subject: "new email",         emailto: "mail1@mail.com",         emailfrom: "mail1@mail.com",         html: 'you have new subscriber' + $scope.emailfield      });      $scope.sendpost = function () {          $http({             url: 'http://my.azurewebsites.net/mail.php',             method: "post",             headers: {                 'content-type': 'application/json; charset=utf-8'             },             data: {                 "req": dataforadminnewsletter             }         });      } }); 

as result have got next error: xmlhttprequest cannot load my.azurewebsites.net/mail.php. response preflight request doesn't pass access control check: no 'access-control-allow-origin' header present on requested resource. origin 'localhost:11708' therefore not allowed access.

i can not change code on server side. can me resolve issue angular?

thank you.

i using lines in apache virtual host conf,

<ifmodule mod_rewrite.c>     rewriteengine on     rewritecond %{request_method} options      rewriterule ^(.*)$ $1 [r=200,l] </ifmodule>   <ifmodule mod_headers.c>     header set access-control-allow-origin "*"     header set access-control-allow-methods "get, post, put, patch, delete, options"  </ifmodule> 

and works charm, may help.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -