2014-01-10 2 views
0

jquery를 처음 사용합니다. html과 jquery를 사용하여 http://www.webservicex.net/geoipservice.asmx?op=GetGeoIP의 웹 서비스를 사용하려고합니다. 나는 다음과 같은 코드를 작성했다 :jquery를 사용하여 웹 서비스를 사용하려고 할 때 오류가 발생했습니다.

<!DOCTYPE html> 
    <page language ="html" validateRequest="false"> 
    <html> 
<head> 
    <title>Hello There</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> type="text/javascript" > </script> 
</head> 
<body> 
     <div> 
     IPAddress: <input type="text" name="id" id="theId"/> 
     <br /> 
     <button id="getRemoteResponseBt">Get remote response</button> 
     </div> 
     <script> 
     var url='http://www.webservicex.net/geoipservice.asmx?op=GetGeoIP'; 
     var dataMessage= 
     '<?xml version="1.0" encoding="utf-8"?> \ 
     <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> \ 
     <soap:Body> \ 
     <GetGeoIP xmlns="http://www.webservicex.net/"> \ 
     <IPAddress>+theId.val()+</IPAddress> \ 
     </GetGeoIP> \ 
     </soap:Body> \ 
     </soap:Envelope>'; 

$("#getRemoteResponseBt").click(function() { 
    $.ajax({ 
    url: url, 
    dataType: "xml", 
    data: dataMessage, 
    processData: false, 
    contentType: "text/xml; charset=\"utf-8\"", 
    success: function(text) { 
     var xml = $(text); 
     var id = xml.find('IP').text(); 
     var name = xml.find('CountryName').text(); 
     var code = xml.find('CountryCode').text(); 
     alert("Result:" + id +name+code); 
    }, 
    error:function (xhr) { 
     alert(xhr.responseText+"Error"); 
    } 
    }); 
return false; 
});   
</script> 
</body> 
</html> 

그러나 내가 버튼을 클릭 할 때마다 에러가 난다. 아무도 코드 작동에 도움을 줄 수 있습니까? 도메인에서 도메인 간 게시물을 시도 http://www.webservicex.net/geoipservice.asmx?op=GetGeoIP

+0

* 정확히 * 오류가 있습니까? – deceze

+0

빠른 답장을 보내 주셔서 감사합니다. 경고에서 '정의되지 않은'오류가 발생합니다. 이 코드를 html 파일로 실행하여 확인할 수 있습니다. – user3181494

+0

성공 함수가 호출 되었습니까? 아니면 오류 기능? 텍스트의 내용은 무엇입니까? 'xml'의 내용? 'id'의 내용? '이름'? '코드'? – Ishtar

답변

1

받는 서버가 그것을하지 않는, CORS를 구현하지 않는 브라우저의 same origin policy으로 금지되어 webservicex.net에 : 웹 서비스에 대한 설명은 링크를 확인하십시오.

API는 동일한 원본이 적용되지 않는 서버 측 스크립트에서 호출되도록 설계되었으므로 도메인에서 대체 서비스를 찾거나 프록시 스크립트를 만들어야합니다.

+1

[여기] (http://jsfiddle.net/wE2vS/)는 오류를보기 위해 내가 수행 한 피들입니다. 내가 브라우저의 콘솔에서 오류를보고 싶다면 여기에 둡니다. – Grooveek

관련 문제