2013-06-26 5 views
1

자바, HTML 및 CSS를 사용하여 모바일 응용 프로그램을 개발 중입니다. 그 응용 프로그램에서 웹 서비스를 사용해야합니다. 자바 스크립트에서 웹 서비스를 소비하는 방법을 보여주기 위해 샘플 데모를하고 있습니다. JSON 응답을 반환하는 url이 있습니다. jQuery를 사용하여이 응답을 표시해야합니다. jscript에서 json 응답을 표시 할 수 없습니다.

은 JSON 응답

http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo

tje 반환 URL과 응답은 다음과 같습니다

{ 
    "status": { 
     "message":"the daily limit of 30000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application.", 
     "value":18 
    } 
} 

하지만 난이 사용하는 자바 스크립트를 표시하는 방법을 모르겠어요. 내가 코드를 아래로 시도하지만

$(document).ready(function() 
    { 
     $("#btnConvert").click(function() 
     { 

      $.ajax({ 
      type: "POST", 
      url:"http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo", 
      dataType: "jsonp", 
      success: function(data) 
      { 
       $('#currency_converter_result').html(data); 
       alert(""+data); 

      } 
      }); 
     }); 
    }); 

그것은 [object object]와 경고 상자를 보여줍니다 작동하지 도다. 자바 스크립트를 사용하여 json 응답을 표시하는 방법에 대해 도움을 주시겠습니까?

당신은이 같은 개체의 메시지를 표시 할 수 있습니다

답변

1

:

alert(data.status.message); 
+0

감사합니다. 잘 작동합니다. –

+0

잘 작동 했으므로 t를 사용해보세요. 파이어 폭스의 Firebug와 같은 ools는 객체 내부의 내용을 쉽게 볼 수 있으며 예를 들어 콘솔 로깅과 같은 구조로되어 있는지 확인할 수 있습니다. – Francodi

1

개체 data 다음 속성을 포함을 :

: 당신이 사용해야하는 값을 추출하기 위해

- data 
    -- status 
     -- message: "the daily limit of 30000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application." 
     -- value: 18 

그래서

data.status.message // Returns the String 
data.status.value // Returns the code number 
관련 문제