2012-05-04 2 views
0

안녕하세요 저는 ajax 요청에서 나머지 webservice를 호출하려고했습니다.ajax 호출이 나머지 웹 서비스를 호출하는 동안 실행하지 못했습니다.

다음은 코드를 시도한 것입니다.

function callRestService(){ 
     var xmlhttp; 
     if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
     }else{// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     $.ajax({ 
       xmlhttp.open("GET","http://mywebservice/test/",false); 
       xmlhttp.send(); 
       alert(xmlhttp.responseText); 
     }); 
    } 

그리고이 코드를 실행하는 동안 점점 오류 오전 다음/내가

$.ajax({ 
       type: "GET", 
       url: "http://mywebservice/test/", 
       cache: false, 
       success: function(data){ 
        alert(data); 
        //var obj = eval(data); 
       }, 
       error: function (msg, url, line) { 
        alert('error trapped in error: function(msg, url, line)'); 
        alert('msg = ' + msg + ', url = ' + url + ', line = ' + line); 
       } 
     }); 

을 다음과 같이하고, 위의 경우 제어에 뭔가를 시도 첫 번째 경우에

missing : after property id 
[Break On This Error] 

xmlhttp.open("GET","http://mywebservice/test.. 

/AjaxC...ervice/ (line 30, col 13) 

오류에 온다 차단하지만, 그 이유가 무엇인지 알지 못했습니다. 그래서 제가 첫 번째 경우를 시도했습니다.

이 코드에 문제가 있습니까? ?? 누구도이 문제를 해결할 수 있습니까?

답변

0

코드가 잘못되었습니다. $.ajax jQuery를 아약스 호출이라고 가정하면, 다음 코드는 다음과 같아야합니다

function CallRestService() { 
    $.ajax({url:'http://mywebservice/test'}).done(function(data) { 
     alert(data); 
    }) 
); 
} 

당신이 JQuery와 아약스 호출을 사용하는 경우 XML을 HTTP 요청을 만들 필요가 없습니다. 참고 사항 : http://api.jquery.com/jQuery.ajax

당신이 jQuery를 사용하지 않으려면

: 여기

function callRestService(){ 
    var xmlhttp; 
    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    }else{// code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    xmlhttp.open("GET","http://mywebservice/test/",false); 
    xmlhttp.send(); 
    if (xmlhttp.status == "200") { 
     alert(xmlhttp.responseText); 
    } 
} 

참조 : https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest

당신이 크로스 도메인 호출을 사용하는 경우 여기를 참조 : jQuery AJAX cross domain

+0

예를. 나도 알아,하지만 그게 나를 위해 오류를 일으키는 이유는 내가이 같은 테스트. 내가 아약스 자체로 시도한 게시 할 것입니다. –

+0

jquery없이 답변을 업데이트했습니다. – WojtekT

+0

나는 또한 귀하의 업데이트 된 코드를 시도했다. 그 경우 다음과 같은 오류가 발생했습니다. 구성 요소는 오류 코드를 반환했습니다 : 0x80004005 (NS_ERROR_FAILURE) [Break On this Error] \t xmlhttp.send(); –

관련 문제