2017-12-07 2 views
0

JS 함수를 호출 할 때 게시 요청을 보내려고합니다. 브라우저에서 나는 보낼 것이다 :Javascript serialized POST

http://myuser:[email protected]:80/api/callAction?deviceID=185&name=turnOn 

이 작동합니다. 아직 내 코드에서는 그렇지 않습니다.

참고 사항 : - Chrome에서 오류가 발생합니다. 요청이 액세스 제어를 통과하지 못합니다. Chrome에서이 기능을 사용 중지하면이 오류가 표시되지 않지만 아직 서버의 응답이 표시되지 않습니다.

<script type="text/javascript"> 

    function changestate() { 
     var http = new XMLHttpRequest(); 
     http.withCredentials = true; 
     var user = "bassie" 
     var pass = "password" 
     var url = "http://hc2/api/callAction"; 
     var params = "deviceID=185&name=turnOff"; 
     http.open("POST", url, true); 
     http.setRequestHeader("Authorization", "Basic " + user + ":" + pass); 
     //Send the proper header information along with the request 
     http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     alert(http.responseText); 
     http.onreadystatechange = function() {//Call a function when the state changes. 
      if(http.readyState == 4 && http.status == 200) { 
        alert(http.responseText); 
      } 
     } 
     http.send(params); 
    } 

</script> 
+0

이'가 자바 스크립트와 웹 페이지를 포함하는 서버의 이름을 hc2'이다 : 그것의 요점 아래? – Barmar

+0

문제를 조사해 주셔서 감사합니다. HC2는 사후 요청 (대상 서버)을 가져 오는 서버의 이름입니다. – Bassie

+0

요청을 보내는 서버의 이름이 아니라면 도메인 간 AJAX를 실행하고있는 것입니다. – Barmar

답변

1

브라우저의 위치에 URL을 퍼팅에 상응는 GET 요청하지 POST입니다.

도메인 간 요청을 보내므로 원본 서버에서 프록시를 통해 릴레이하지 않는 한 응답을 읽을 수 없습니다. 따라서 http.responseText를 읽을 수없고 onreadystatechange 함수를 간단히 생략 할 수 있습니다. 당신은 그냥 가정해야합니다.

function changestate() { 
    var http = new XMLHttpRequest(); 
    http.withCredentials = true; 
    var user = "bassie" 
    var pass = "password" 
    var url = "http://hc2/api/callAction"; 
    var params = "deviceID=185&name=turnOff"; 
    http.open("GET", url + "?" + params, true); 
    http.setRequestHeader("Authorization", "Basic " + user + ":" + pass); 
    http.send(); 
} 
0

결국 같은 종류의 프록시를 만드는 것을 끝내게됩니다. 이것이 주요 구성 요소였습니다. 예제에서 (내 스크립트는 HTTP 요청을 얻음) 출력을 얻습니다.

req = urllib.request.Request('http://hc2:80/api/callAction?param1=1&param2=2') 
    credentials = ('%s:%s' % ('user', 'password')) 
    encoded_credentials = base64.b64encode(credentials.encode('ascii')) 
    req.add_header('Authorization', 'Basic %s' % 
    encoded_credentials.decode("ascii")) 
    response = urllib.request.urlopen(req) 
관련 문제