简体   繁体   中英

Consuming RESTful Web Service using jQuery Ajax Client

I am trying to set-up a jQuery Client to Consume the MedlinePlus Web Service ( http://www.nlm.nih.gov/medlineplus/webservices.html ) but its the first time I have ever done so and cannot get it to work for me :( Any help would be appreciated.

<!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>

Please provide the problems you are getting. I think you will get http://en.wikipedia.org/wiki/Cross-site_scripting issues since the url is accessing from out side of wsearch.nlm.nih.gov context. If you are sure that it is a rest webservice call then instead of a direct webservice call, Do a server call(Internal Call to Servlet/ASP/PHP by matching some URL pattern) and then invoke the web service url from there by using third party RestClient libraries . Eg. RestClient for JBoss & Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM