-1

I would like to add custom header for Ajax CORS request .

my server is PHP lumen and proxy server is nginx ,the path bootstrap/app.php is my project configure enter image description here

I have got this and this ,but they aren't helpful.

I'm wonder is there need some configure of the server or define in my script. here is my code :

$.ajax({
    url: "http://xx.api:8000/user/test",
    dataType: 'json',
    type: 'GET',
    headers: {
        "mark": "111",  
    },
    success: function() {
        console.info('sucess');
    },
    error: function() {
        console.info("error");
    }
});

when I build in chrome ,the console log is :

OPTIONS http://xx.api:8000/user/test 405 (Method Not Allowed)
XMLHttpRequest cannot load http://xx.api:8000/user/test. Invalid HTTP status code 405

the Request Headers is

OPTIONS /user/test HTTP/1.1
Host: xx.api:8000
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: null
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36
Access-Control-Request-Headers: accept, mark
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6

the Response Headers is

HTTP/1.1 405 Method Not Allowed
Server: nginx
Content-Type: application/json;charset=utf8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.6.14
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: *
Access-Control-Allow-Headers: *
allow: GET
Cache-Control: no-cache, private
date: Tue, 22 Dec 2015 02:16:16 GMT

Any suggestions ?

Community
  • 1
  • 1
HuangMin
  • 103
  • 1
  • 7
  • What is your web server? – Rajesh Manilal Dec 21 '15 at 11:12
  • "xhr.setRequestHeader("mark", "authorizationToken"); // is no work" — What does "no work" mean? Does the request get made? Do you get a response? Does the console show error messages? **use your browser's developer tools** – Quentin Dec 21 '15 at 11:51
  • @RajeshManilal the web server is PHP lumen and proxy server is nginx, I had edit my question.thank you ! – HuangMin Dec 22 '15 at 02:24
  • @Quentin thank about you criticism, I'm had edit my question, add response and request . thank you ! – HuangMin Dec 22 '15 at 02:29

1 Answers1

1

If your web server is Apache

Add the following in .htaccess file and place it in root directory of your application(http://api.test)

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "POST"

For IIS7

Create Web.config file on the root directory of your application and add the following XML code in it

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>
</configuration>
Rajesh Manilal
  • 1,104
  • 9
  • 20