티스토리 뷰

오타변환, 추천검색어를 따로 구현 후 합쳐봤다.
영타입력으로 잘못 입력해도 추천검색어가 나온다.


<script language="javascript" type="text/javascript">
//<![CDATA
	var url = 'recmd.asp';
	var q = encodeURIComponent("zjfldj");
	$(function(){
		$.getJSON(url, { query:q }, function(json){
			if(!json) { $('#result').text('응답없음'); return false; }
			var str = "";
			var cnt = json.result.item.length;
			for(var i=0; i<cnt; i++) {
				str += "<li>"+ json.result.item[i] +"</li>";
			}
			$('#result').html(str);
		});
	});
//]]>
</script>

<div id="item-count"></div>
<ul id="result"></ul>

처리

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% '// 네이버 오타변환 & 추천검색어 API
Session.CodePage = "65001"	'"949"
Response.CharSet = "UTF-8"	'"euc-kr"
Response.addHeader "pragma", "no-cache"
Response.addHeader "cache-control", "private"
Response.AddHeader "Expires","0"
Response.CacheControl = "no-cache"
Response.ContentType = "application/json"

Dim query, key, target, getUrl

query = Request.Querystring("query")
If query = "" Then Response.write(""): Response.End

key = "{API KEY}"

'// 오타변환
target = "errata"
getUrl = "http://openapi.naver.com/search"
getUrl = getUrl &"?key="& key &"&target="& target &"&query="& query

Set objHTTP = CreateObject("Microsoft.XMLHTTP")
objHTTP.Open "get", getUrl, False
objHTTP.Send
xmlData = objHTTP.responseText
if xmlData="" then Response.write("Naver url is not available"): Response.End
Set objHTTP = Nothing
'Response.write(Replace(Replace(xmlData, "<", "<"), ">", ">")): Response.End	'// debug
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.loadXML(xmlData)
Set colNodes = xmlDoc.selectSingleNode("/result/item/errata")
Set xmlDoc = Nothing
errata = colNodes.text
Set colNodes = Nothing
If errata <> "" Then query = encodeURI(errata)

'// 추천검색어
target = "recmd"
getUrl = "http://openapi.naver.com/search"
getUrl = getUrl &"?key="& key &"&target="& target &"&query="& query

Set objHTTP = CreateObject("Microsoft.XMLHTTP")
objHTTP.Open "get", getUrl, False
objHTTP.Send
xmlData = objHTTP.responseText
if xmlData="" then Response.write("Naver url is not available"): Response.End
Set objHTTP = Nothing

Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.loadXML(xmlData)
Set colNodes = xmlDoc.selectNodes("/result/item")
if colNodes.length = 0 then Response.write("recmd item does not exists."): Response.End
Set xmlDoc = Nothing

json = ""
For Each objNode in colNodes
   json = json &""""& objNode.text &""","
Next
If Len(json) = 0 Then Response.write("XML parsing is not available"): Response.End
json = Left(json, Len(json)-1)
Set colNodes = Nothing

Response.Write("{""result"":{""item"":["& json &"]}}")
%>
<script language="javascript" runat="server">
function encodeURI(str)
{
	return encodeURIComponent(str);
}
</script>
댓글
댓글쓰기 폼
Total
931,020
Today
0
Yesterday
185
«   2021/08   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
글 보관함