2016-11-28 1 views
0

서버에 로컬 파일에서 데이터 아약스를 가져오고 싶습니다. I은 ​​다음과 JS 년을 writting 해요 :순수 자바 스크립트로 서버 로컬 파일에서 데이터 아약스 가져 오기

var UrlDetails = 'http://192.xxx.x.xxx:7000'; 
function createCORSRequest(method, url, asynch) { 
    var xhr = new XMLHttpRequest(); 
    if ("withCredentials" in xhr) { 
     // XHR for Chrome/Firefox/Opera/Safari. 
     xhr.open(method, url, true); 
     xhr.setRequestHeader('MEDIBOX', 'login'); 
     xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); 
    } else if (typeof XDomainRequest != "undefined") { 
     // XDomainRequest for IE. 
     xhr = new XDomainRequest(); 
     xhr.open(method, url, asynch); 
    } else { 
     // CORS not supported. 
     xhr = null; 
    } 
    return xhr; 
} 

function getDoLogin(e, callback) { 
    var url = UrlDetails + '/api/loginGET?username=' + e.id + '&password=' + e.password + '&f=json'; 
    var xhr = createCORSRequest('GET', url, true); 
    if (xhr) { 
     xhr.onreadystatechange = function() { 
      try { 
       if (xhr.readyState === XMLHttpRequest.DONE) { 
        if (xhr.status === 200) { 
         callback(JSON.parse(xhr.responseText)); 
        } else { 
         xhr.status === 403 ? modalShow() : errorServer(xhr.status); 
        } 
       } 
      } 
      catch (e) { 
       alert('Caught Exception: ' + e.description); 
      } 
     }; 
     xhr.send(); 
    } 
} 

내가 기능 getDoLogin(e, f);를 호출 내가 크롬에서 오류가 발생합니다 : XMLHttpRequest의은 http://192.xxx.x.xxx:7000/api/loginGET?username=1&password=1&f=json를로드 할 수 없습니다. 내가 정확히 실행하는 JS에서 작성하는 방법을 모르는

function getDoLoginJQuery(e) { 
    return $.Deferred(function (d) { 
     $.ajax({ 
      type: 'get', 
      url: UrlDetails +'/loginGET?username=' + e.id + '&password=' + e.password + '&f=json', 
      cache: 'false', 
      dataType: 'json' 
     }).done(function (a, b) { 
      d.resolve(a) 
     }).fail(function (a, b, c) { 
      d.reject(a, b, c); 
     }) 
    }) 
} 

: 프리 플라이트에 대한 응답 (405)

그것은 단지 jQuery로 실행중인 유효하지 않은 HTTP 상태 코드가 있습니다. 는 (미안 내 영어가 좋지 않다..)

+0

어떤 오류 메시지가 표시됩니까? –

+3

'나는 어떻게 JS로 작성 해야할지 모르겠다. '- 프로그래밍에 대한 도움을 요청할 수있는 오버플로 스택을 환영합니다. 프로그래밍 가이드를 얻을 곳이 아니라는 것을 알아 두십시오. 자바 스크립트 작성법을 모른다면 1 단계는 자바 스크립트 작성법을 배우는 것입니다. 이 사이트에서 많은 작업을 수행 할 수 있습니다. –

+0

내 질문을 편집했습니다. 나 좀 도와 줄 수있어? –

답변

0

시도는 다음과 같이, setRequestHeader을 제거 :

function createCORSRequest(method, url, asynch) { 
    var xhr = new XMLHttpRequest(); 
    if ("withCredentials" in xhr) { 
     // XHR for Chrome/Firefox/Opera/Safari. 
     xhr.open(method, url, true); 
     // xhr.setRequestHeader('MEDIBOX', 'login'); 
     // xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); 
    } else if (typeof XDomainRequest != "undefined") { 
     // XDomainRequest for IE. 
     xhr = new XDomainRequest(); 
     xhr.open(method, url, asynch); 
    } else { 
     // CORS not supported. 
     xhr = null; 
    } 
    return xhr; 
} 

행운을 빕니다.

+0

내 질문을 편집했습니다. 나 좀 도와 줄 수있어? –

+0

고맙습니다. 실행 중입니다. POST로 실행하려면 어떻게 실행합니까? –

+0

'getDoLogin()'함수에서'createCORSRequest ('GET', url, true);를'createCORSRequest ('POST', url, true);로 변경하십시오. –

관련 문제