2014-01-27 2 views
3

내 통신을 처리하기 위해 설치에게 도장 모듈을했습니다 수 있습니다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 사람들은이 작업을 수행하는 방법을 알고 있습니까?

+0

문제는 해당 서버에서 발생할 수 있습니다. 실패한 시도에서 서버의 응답 헤더와 함께 요청 헤더를 표시하십시오. –

+0

jquery 요청 : http://imgur.com/RgV8ZBc 및 도조 요청 : http://imgur.com/5o1sdQC 이미지에 대해 진심으로 사과드립니다. 서버에 액세스 할 수 없습니다. 내가 실제로 보여줄 수있는 것은 요청이 jquery에서 어떻게 작동하고 devtools에서 서버의 응답인지입니다. –

+1

실제 응답 헤더를 제공하기 위해 서버에 액세스 할 필요가 없습니다. 클라이언트와 서버간에 투명한 프록시를 삽입하여 정확하게 기록 할 수 있습니다. CORS 요청을 제대로 확인하지 못하는 응답이있는 CORS 요청의 경우 헤더를 올바르게보고하도록 브라우저를 신뢰할 수 없습니다. 믹스에서 Dojo와 함께 보내지는 프리 플라이트의 요청 헤더와 임시 헤더만으로는 요청이 실패한 이유를 말하기 어렵습니다. 실제 요청 및 응답 헤더를 살펴보면이 모든 사항이 매우 명확해질 것입니다. –

답변

2

빈 문자열은 null과 다릅니다. 프리 플라이트 요청을 피하려면 null이 아닌 X-Requested-With를 ""으로 설정해야합니다. 서버가 X-Requested-With 헤더를 허용하지 않는 한, 항상 프리 플라이트를 유발하는 커스텀 헤더를 보내기 때문에 중요하지 않습니다.

+0

빈 문자열과 null을 모두 시도했습니다. 같은 결과. 사용자 정의 헤더 'X-AUTH'가 프리 플라이트를 트리거하고 있지만 jquery 요청이 프리 플라이트를 전달한다는 것을 알고 있습니다. –

+0

이 요청은 Content-Type으로 인해 X-Requested-With 헤더가없는 경우에도 미리 채 웁니다. –

+0

그렇지만 토큰 ('X-AUTH'헤더)을 보내야하므로 선택의 여지가 없습니다. jQuery 예제에서도 동일한 작업을 수행합니다. –

2

나는 dojo.xhrGet과 'Content-Type'에서 비슷한 경험을했다. 'application/json'이 문제였다. 이 일 것으로 도메인 간 후

dojo.xhrGet({ 
    url: url, 
    headers: { 
     'X-Requested-With': null, 
     'Content-Type': 'text/plain' 
    }, 
    load: function(responseText) { 
     var results = JSON.parse(responseText); 
    ... 
    } 
}); 

을하고는 JSON 객체로 결과를 처리 : I 따라서, 실제로 크로스 도메인을 작동하려면 내가 일반 텍스트를 다시 얻고 있었다 척을 변경했다.

0
  var syncCall = true; 
      if(dojo.isFF) 
      syncCall = false; 
      xhr(url, { 
      method : 'GET', 
      handleAs : 'json', 
      withCredentials : true, 
      sync : syncCall, 
      headers : { 
       'X-XSRF-Token' : settings.XSRF_TOKEN, 
       'Content-Type' : 'application/json' 
      } 
0

당신은 단순히 CONNECT가, 머리를 갖다 댔, 옵션 등 모든 HTTP 요청을 GET, PUT, DELETE하는 도장/요청/XHR 모듈을 사용할 수 있습니다, A와 등

패스 HTTP 메소드 이름을 추적 문자열 (모두 대문자)은 다음과 같습니다.

require(['dojo/request/xhr'], function(xhr){ 
    xhr(url, { 
     handleAs : "json", 
     method : 'OPTIONS', 
     data : formDataAsJson, 
     headers : { 
      'Content-Type' : 'application/json' 
     } 
    }).then(
     function(data) { 
      console.log("response data is: "+ JSON.stringify(data)); 
     }, 
     function(err) { 
      console.log("The err is: "+ JSON.stringify(err)); 
     }, 
     function(evt) { 
      console.log("The evt is: "+ JSON.stringify(evt)); 
     } 
    ); 
}); 
관련 문제