views:

42

answers:

2

I have a webservice webmethod which saves the xml output to destination

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public XmlDocument GetList(
  string keyword1, string streetname, string lat, string lng, string radius)
{      
  XmlDocument xmlDoc = CreateXML( keyword1,streetname,lat,lng,radius);

  //save file to application folder which will be refferd by client application
  xmlDoc.Save(Server.MapPath("~/Block3.xml"));
  return xmldoc;       
}

I am trying to refer from client side using the following code in searchurl

function searchLocationsNear() {
  var radius = document.getElementById('radiusSelect').value;             
  var searchUrl ="http://localhost:2385/block/Block3.xml"; //reference for xml file stored in application folder

  GDownloadUrl(searchUrl, function(data) {
    var xml = GXml.parse(data);
+1  A: 

One way could be to:

  1. Call the web method from javascript
  2. Use XmlDocument object in javascript
KMan
Thanks for your reply,I wll be using html and jquery on my clientside
mahesh
+1  A: 

The following is a simple example of how you might use jQuery to call the server side WebMethod from the client side. This code assumes that you are hosting the searchLocationNear(...) method in WebService1.asmx

  function searchLocationNear() {
    // Get the radius using jQuery
    var radius = $("#radiusSelect").val();

    // Make Ajax call using jQuery
    $.ajax({
      type: "POST",
      data: "keyword1=&streetname=&lat=&lng=&radius=" + radius,
      url: "WebService1.asmx/GetList",
      success: function (response) {
        var xml = GXml.parse(response.xml);
      },
      error: function (response) {
        alert(response.responseText);
      }
    });
  }

Here $.ajax is used to make a call to the GetList web method, and the XML is extracted from the response. This works find if you are returning an XmlDocument on the server side as in your case.

Chris Taylor
Thanks for the reply ,can i pass json constructed data to webservice and get back xml as response ,in that case should my datatype be xml or json
mahesh
@mahesh, yes you can pass JSON data and get back the XML, just add `datatype: "json", contentType: "application/json; charset=utf-8"` to the above ajax call and format the data using JSON format like this `data: "{keyword1: '', streetname: '', lat: '', lng: '', radius:" + radius + "}"`
Chris Taylor
Thanks a lot for your suggestion it helped me a lot.
mahesh
Is their any problem with my webservice code
mahesh
This is my webservice code
mahesh
[WebMethod][ScriptMethod(ResponseFormat = ResponseFormat.Xml)]public XmlDocument GetList( string keyword1, string streetname, string lat, string lng, string radius){ XmlDocument xmlDoc = CreateXML( keyword1,streetname,lat,lng,radius);return xmldoc; }
mahesh
@mahesh, is there a specific error you are getting? The above looks fine, did you add `[ScriptService]` to the actual service class?
Chris Taylor
Ya i have added using System.Web.Script.Services; Its working fine now thank you very much.
mahesh
Please let me know if their are any good articles which uses jquery in html page on client side and asp.net webservices on serverside
mahesh