【问题标题】:Consuming RESTful Web Service using jQuery Ajax Client使用 jQuery Ajax 客户端使用 RESTful Web 服务
【发布时间】:2014-01-31 09:32:46
【问题描述】:

我正在尝试设置一个 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>

【问题讨论】:

  • 我认为您编写 URL 的方式不符合 REST。不确定这是否是问题所在。你检查过 jQuery 的主页(api.jquery.com/jquery.ajax)吗,函数 ajax() 的所有参数都在那里简短地描述。也许您可以在阅读后在这里提出更具体的问题。

标签: jquery ajax web-services rest


【解决方案1】:

请提供您遇到的问题。我认为您会遇到 http://en.wikipedia.org/wiki/Cross-site_scripting 问题,因为 URL 是从 wsearch.nlm.nih.gov 上下文之外访问的。如果您确定它是一个 REST Web 服务调用,而不是直接 Web 服务调用,请执行服务器调用(通过匹配某些 URL 模式对 Servlet/ASP/PHP 进行内部调用),然后使用 第三方 RestClient 库。例如。用于 JBoss 和 Java 的 RestClient

【讨论】:

  • 嗨,Mahesh - 你可能就在那儿。今晚我会再看一遍。
猜你喜欢
  • 2012-05-10
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 2011-08-22
  • 2011-03-26
  • 1970-01-01
  • 2013-06-03
  • 1970-01-01
相关资源
最近更新 更多