내 통신을 처리하기 위해 설치에게 도장 모듈을했습니다 수 있습니다Dojo는 CORS 요청을 할 수 없습니다. jQuery를
define(["dojo/request/xhr", "dojo/json"],
function(xhr, JSON) {
return {
getJson: function(url) {
return xhr.get(url, {handleAs:'json', headers: {"X-Requested-With": ""}});
},
postJson: function(url, postData) {
return xhr(url, {
method: 'POST',
handleAs: "json",
data: JSON.stringify(postData),
headers: {"X-Requested-With": "", "Content-Type":"application/json"}
})
},
getSecure: function(url, token) {
return xhr.get(url, {handleAs:'json', headers: {"X-AUTH": token, "X-Requested-With": "", "Content-Type":"application/json" }});
},
postSecure: function(url, postData, token) {
return xhr(url, {
method: 'POST',
handleAs: 'json',
data: JSON.stringify(postData),
headers: {"X-Requested-With": "", "Content-Type":"application/json", "X-AUTH": token}
});
}
};
});
요청을 보내는 동안이 옵션은 거의 즉시 실패합니다. Postman에서 API가 제대로 작동하는지 확인하기 위해 요청을 시도했습니다. 그런 다음 야생 머리카락을 가지고 jQuery에서 빠른 테스트를 만들었습니다.
$.ajax({
url: 'https://someurl.url/auth/get_token',
type: 'post',
data: JSON.stringify({username:"user", password:"pass"}),
contentType: 'application/json',
dataType: 'json' ,
xhrFields: {
withCredentials: false
},
success: function(json) {
console.log(json);
$.ajax({
url: 'https://someurl.url/api/service/' + json.results.somevalue,
type: 'GET',
headers: { 'X-AUTH': json.results.token },
contentType: 'application/json; charset=utf-8',
dataType: 'json' ,
success: function(json) {
console.log(json);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log("error :"+XMLHttpRequest.responseText);
}
});
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log("error :"+XMLHttpRequest.responseText);
}
잘 작동합니다. 나는 Dojo가 'X-Requested-width'헤더를 보내는 것으로 1 년 전에 문제를 겪고 있었다는 것을 알고 있습니다. 그러나 그것을 배제하고 그것을 보내려고하지 않습니다. 웹 요청을하기 위해서만 내 애플 리케이션에 종속성으로 jQuery를 포함하고 싶지 않기 때문에 머리말을 꺼내고있다. Dojo가이를 수행 할 수 있어야한다. 밖에있는 Dojo 사람들은이 작업을 수행하는 방법을 알고 있습니까?
문제는 해당 서버에서 발생할 수 있습니다. 실패한 시도에서 서버의 응답 헤더와 함께 요청 헤더를 표시하십시오. –
jquery 요청 : http://imgur.com/RgV8ZBc 및 도조 요청 : http://imgur.com/5o1sdQC 이미지에 대해 진심으로 사과드립니다. 서버에 액세스 할 수 없습니다. 내가 실제로 보여줄 수있는 것은 요청이 jquery에서 어떻게 작동하고 devtools에서 서버의 응답인지입니다. –
실제 응답 헤더를 제공하기 위해 서버에 액세스 할 필요가 없습니다. 클라이언트와 서버간에 투명한 프록시를 삽입하여 정확하게 기록 할 수 있습니다. CORS 요청을 제대로 확인하지 못하는 응답이있는 CORS 요청의 경우 헤더를 올바르게보고하도록 브라우저를 신뢰할 수 없습니다. 믹스에서 Dojo와 함께 보내지는 프리 플라이트의 요청 헤더와 임시 헤더만으로는 요청이 실패한 이유를 말하기 어렵습니다. 실제 요청 및 응답 헤더를 살펴보면이 모든 사항이 매우 명확해질 것입니다. –