简体   繁体   中英

how to call a webservice using html and javascript

I am creating a mobile website using html5. Currently i use javascript to populate my fields with static data. I have also created a webservice using .net and c#, and a database using mysql. My webservice can query the data base properly. Does anyone know how to call the webservice from a javascript function?

Personally, I would use jquery for it. There is a pretty good article about it here: http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/

Basically, you call the webservice, with the Ajax() function, and hook up some functions to its success (and possible failed) events, like in this example:

$(document).ready(function() {
  $.ajax({
    type: "POST",
    url: "RSSReader.asmx/GetRSSReader",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      // Hide the fake progress indicator graphic.
      $('#RSSContent').removeClass('loading');

      // Insert the returned HTML into the <div>.
      $('#RSSContent').html(msg.d);
    }
  });
});

You can then get your webservice output, from the "msg" in the sucess function.

The easiest way is using AJAX - most libraries like jQuery have direct support. Note that by design, browsers do not allow you to access a web service that's running on a different domain to the host page (see this ) but there are ways round this using techniques like JSONP .

The quickest way to get started is probably to use jQuery's ajax() function. Hope this helps!

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