使用jQuery Ajax Client使用RESTful Web服务

时间:2014-01-31 09:32:46

标签: jquery ajax web-services rest

我正在尝试设置一个jQuery客户端来使用MedlinePlus Web服务(http://www.nlm.nih.gov/medlineplus/webservices.html),但这是我第一次这样做而无法让它为我工作:( 任何帮助将不胜感激。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Health Topics Web Service</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script language="javascript">
//http://www.nlm.nih.gov/medlineplus/webservices.html
$(document).ready(function(){
$('select').change(function(){
      $.ajax(
        { url: 'http://wsearch.nlm.nih.gov/ws/query?db=healthTopics&term='+this.value+'&rettype=topic',
        complete: function ( jsXHR, textStatus ) {
        var xmlResponse = $.parseXML(jsXHR.responseText),
            $xml = $(xmlResponse),
            $main = $xml.find('content name="title"'),
            $description = $xml.find('content name="FullSummary"'),
            $('h3#title').html("Title: "+$main.title());
            $('h3#FullSummary').html("Summary: "+$description.FullSummary());
          }
        }
      );

    });
});//ready
</script>
</head>
<body>
<div class="medical">
<h2>Health Topics Web Service</h2>
Select Word : <select name="title" style="width:120px;margin-left:10px;">
  <option value="">Select one</option>
  <option value="Diabetes">Diabetes</option>
  <option value="Migraine">Migraine</option>
</select>
<p id="vBox">
<h3 id="title"></h3>
<h3 id="FullSummary"></h3>
</p>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

请提供您遇到的问题。我认为您将收到 http://en.wikipedia.org/wiki/Cross-site_scripting 问题,因为网址是从wsearch.nlm.nih.gov上下文访问的。如果您确定它是一个休息webservice调用,而不是直接的webservice调用,请执行服务器调用(通过匹配某些URL模式对Servlet / ASP / PHP进行内部调用),然后使用<从那里调用Web服务URL strong>第三方RestClient库。例如。 RestClient for JBoss&amp;爪哇