我想从客户端获取Web API令牌。在IE中工作正常,但在Chrome中它不起作用,因为如果我使用Content-Type“application / x-www-urlencoded”发送POST操作,则客户端不会发送OPTION请求。如果我将Content-Type“application / json”设置为其余的put操作,但它不适用于/ Token(无效类型)。
这是我的客户代码:
function getToken() {
var data = { username: $j('#email').val(), password: $j('#password').val(), grant_type: "password" }
$j.ajax({
url: "http://localhost:59039/Token",
method: "post",
data: data,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
error: function (e) {
alert('Error getting token');
console.log(e);
},
success: function (response) {
saveToken(response.access_token);
console.log(response.access_token);
getData(response.access_token);
}
});
这是请求:
POST ht tp://localhost:59039/Token HTTP/1.1
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Referer: http://localhost/csm_example/ejemplo-formulario/
Accept-Language: es-ES
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Connection: Keep-Alive
Content-Length: 77
DNT: 1
Host: localhost:59039
Pragma: no-cache
username=alvaromb%40pruebas.es&password=Prueba_2015&grant_type=password
这是回应:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 698
Content-Type: application/json;charset=UTF-8
Expires: -1
Server: Microsoft-IIS/10.0
X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNcYWx2YXJvbWJcZG9jdW1lbnRzXHZpc3VhbCBzdHVkaW8gMjAxNVxQcm9qZWN0c1xDU01fV1NcQ1NNX1dTXFRva2Vu?=
X-Powered-By: ASP.NET
Access-Control.Allow-Origin: http://localhost
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE
Date: Wed, 23 Dec 2015 13:48:53 GMT
{"access_token":"DRFMISg0_ruFfh1VEoFz6iy5RHbeOw3bRqG0BANSLmif9EwPfptQesEBZgkYoQRa3UmIUg5Z6LQpTo2jtttZpv6bHPuYSXb-0FJF_RHxwbq9OQHF1uH7FtabdGK0_wXybaFCnHm63K0aQ0LSd4YAn7JkfVdtXod_iWs3CvmTLy8y3a-XlVkNQB7blcsQN-30nJ_TL7SzvxyCpNYHlmbbGQHMVzegvnYy9R86LgnatCcDnrUXoc1VDC6jW5n3RsstEg9HI1ITbUAqp2MMk8ki7HuVjjKSOcBv-dD_jIiaCJ4sxH9qjk1q75iXls2H3GmfddhEW4fzaLGPbw0SFkncuHsRULM8M8m-eGdMIwstEdyjFPb4B9kdEmQLi6dIqJJZ4hWIM0g2kvqIrm5MC8aR5uja1VtDH0HhDuPNz-Q9_hsQmM2PD3h9oV1v71-vfz_94jGcI3n6VwRZxiK0xLgQVu4QYdV3DfFQJZvVAzXVXqPMGATvvipfcsw_p_y_m7bB","token_type":"bearer","expires_in":1209599,"userName":"alvaromb@pruebas.es",".issued":"Wed, 23 Dec 2015 13:47:39 GMT",".expires":"Wed, 06 Jan 2016 13:47:39 GMT"}
服务器代码是使用ASP.NET Web API生成的,启用CORS。如果我使用Content-Type“application / json”发送de PUT操作,结果是正确的,生成OPTION请求。它适用于其他服务,例如/ api / Values:OPTIONS htt p://localhost:59039/api/values HTTP/1.1
Host: localhost:59039
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: htt p://localhost
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Access-Control-Request-Headers: accept, authorization, content-type
Accept: /
Referer: htt p://localhost/csm_example/ejemplo-formulario/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: es-ES,es;q=0.8,en;q=0.6
所以,问题是我无法使用Content-Type“application / json”调用/ Token url,或者当Content-Type为“application / x-www-urlencoded”时,我不能说客户端生成OPTION请求”。有什么建议吗?