아래와 같이 JSON 포스트 응답을 받았습니다. JSON 포스트 응답 데이터를 반복하고 이미지 div에 데이터를 인쇄하고 싶습니다 (그림 참조).
JavaScript를 사용하여 이것이 어떻게 수행되는지 보여줄 수 있습니까? 감사
JSON 포스트 응답을받는 자바 스크립트 코드 :
cordovaHTTP.post(url,data,
function(response) {
alert("Data: " + response.data + "\nStatus: " + response.status);
}
수신 된 요청 후 응답 :
"[\r\n {\r\n \"itemID\": \"12345678\",\r\n \"itemTitle\": \"mango\",\r\n \"itemText\": \"\",\r\n \"ThumbUrl\": \"http://awebsite.com/pics/1.jpg\",\r\n \"Other\": null\r\n },\r\n {\r\n \"itemID\": \"12345679\",\r\n \"itemTitle\": \"orange\",\r\n \"itemText\": \"\",\r\n \"ThumbUrl\": \"http://awebsite.com/pics/2.jpg\",\r\n \"Other\": null\r\n }\r\n]"
인쇄하려는 이미지 div :
<div class ="image">
<a href="javascript:dofunction('./test.php?title=Mango&TargetUrl=http://somesite.com/12345678')">
<img src="http://awebsite.com/pics/1.jpg" alt=".." />
</a>
</div>
<div class ="image">
<a href="javascript:dofunction('./test.php?title=orange&TargetUrl=http://somesite.com/12345679')">
<img src="http://awebsite.com/pics/2.jpg" alt=".." />
</a>
</div>
편집 : 아래 답변을 수락하고 정규식을 사용하여 모든 \ r \ n을 제거하고 모든 itemText 키 값을 "itemtext": "empty"로 변경하여 일부 대체 기능을 사용하여 실제 API 데이터를 확인해야했습니다!
다음과 같이 작성할 수 있습니다.
cordovaHTTP.post(url, data, function(response) {
// first, convert the string to JSON data array structure
var json = $.parseJSON(response.data);
// then loop the single items
for(i in json)
{
// create HTML code
var div = "<div class=\"image\">" +
"<a href=\"javascript:dofunction('./test.php?title=" + json[i].itemTitle + "&TargetUrl=http://somesite.com/" + json[i].itemID + "')\">" +
"<img src=\""+ json[i].ThumbUrl +"\" alt=\"..\" />" +
"</a>" +
"</div>";
// append it inside <body> tag.
$("body").append(div);
}
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다