Есть Api
, запускается в vs 2017 Ctrl+F5
, так же на локальном IIS есть фронтенд.
По нажатию на Войти
должен приходить токен
.
Проверяю в постмене - все работает:.
код ajax
с postman
:
var settings = {
"async": true,
"crossDomain": true,
"url": "https://localhost:44311/api/user/login",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"cache-control": "no-cache",
"Postman-Token": "49834433-34a0-4c59-9593-8ea5bd8515b2"
},
"processData": false,
"data": "{\n\t\"UserName\":\"MARS\",\n\t\n\t\"PasswordHash\":\"123qweASD!\"\n}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
результат:
теперь тоже самое но с ajax запроси по нажатию кнопки войти
:
$.ajax({
async: true,
crossDomain: true,
url: "https://localhost:44311/api/user/login",
method: "POST",
headers: {
"Content-Type": "application/json",
"cache-control": "no-cache",
"Postman-Token": "c236d8f2-4ef0-4187-bfea-494d032e179a",
// "Access-Control-Allow-Credentials": "true"
},
processData: false,
dataType: 'json',
data: { "UserName": "MARS", "PasswordHash": "123qweASD!" },
success: function (res) {
alert(res);
},
error: function () {
alert("error.");
}
});
ошибки:
Access to XMLHttpRequest at 'https://localhost:44311/api/user/login' from origin 'http://localhost:5555' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Upgrade:
Запорос:
$.ajax({
async: true,
// crossDomain: true,
url: "https://localhost:44311/api/user/login",
type: "POST",
headers: {
"Content-Type": "application/json",
"cache-control": "no-cache",
"Postman-Token": "c236d8f2-4ef0-4187-bfea-494d032e179a",
// "Access-Control-Allow-Credentials": "true"
},
processData: false,
jsonp: "callback",
dataType: 'jsonp',
data: { "UserName": "MARS", "PasswordHash": "123qweASD!" },
success: function (res) {
alert(res);
},
error: function () {
alert("error.");
}
});
Что в браузере:
Как правильно сделать ajax запрос???