DooDoo
DooDoo

Reputation: 13437

how to pass a XML parameter to a WEB METHOD using jQuery Ajax

I have a web method that can accept a XElement argument and I want to call it using jQuery Ajax. I can call simple methods using jQuery Ajax with simple argumnets(such as int,string,...), but I don't know how pass to a complex type using jQuery Ajax.


Edit 1)

I have a simple web service :

[WebMethod]
public bool MyGetPassXML(System.Xml.XmlDocument nima)
{
    try
    {
        if (nima == null )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    catch (Exception ex)
    {
        return false;
    }
} 

For the call I've written this code:

var xmlFragment = 'AB';

$("#Button2").click(function() {
    $("#Loader").show();
    $.ajax({
        type: "POST",
        url: "WebService.asmx/MyGetPassXML",
        data: "{'nima':'" + xmlFragment + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            $("#Loader").hide();
            alert('OK');
        }
    });
});

I've test it with Firefox and see request an response with FireBug but I get this error:

{"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Xml.XmlDocument\u0027","StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary2 rawParams)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

I change System.Xml.XmlDocument to XElement but again I get this error. How can I solve this?

Upvotes: 0

Views: 9519

Answers (3)

naugtur
naugtur

Reputation: 16915

What you pass in xmlFragment might not be a correct XML because there is no XML declaration.

Also - If you didn't state that anywhere, I don't think your method consumes JSON

I am not familiar with your server side technology, but if you want to send XML type content there is no way to state it in JSON. You have to send stuff like this:

     $.ajax({
            type: "POST",
            url: "WebService.asmx/MyGetPassXML",
            data: xmlFragment,
            contentType: "application/xml; charset=utf-8", //not sure if charset should be there for xml
            dataType: "json", //data type expected in _RESULT_. I have no idea what your method returns
            success: function(result) {
                $("#Loader").hide();
                alert('OK');
            }
        }); 

And you might have to change something about your method to receive the whole XML. But this is the only commonly avaliable way to send anything to the server with any indication of XML object type.

Upvotes: 0

ScottE
ScottE

Reputation: 21630

Instead of XmlDocument as the web method argument type, I would change it to a string. You can create an XmlDocument from a string by calling .LoadXml(nima).

Edit to answer the request for an example:

Let's say you had a simple class like the following (forgive my c#):

public TestClass {

  public string Var1 { get; set; }
  public string Var2 { get; set; }

  public string void TestClass() 
  {

  }

}

And a web method like the following:

[WebMethod]
public bool MyGetPassJSON(TestClass nima)
{
  // do something
} 

And your javascript / jquery could look like the following:

   $.ajax({
        type: "POST",
        url: "WebService.asmx/MyGetPassJSON",
        data: "{nima: {Var1:'something',Var2:'something else'}}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            $("#Loader").hide();
            alert('OK');
        }
    });

In your case, you could have:

"cities": ["A", "B","C","D"]

This is a very simple example, but much more complex parameters are possible. You can also build up your json as a proper javascript object and use JSON.stringify to send it to your web method.

Upvotes: 2

Bryan Naegele
Bryan Naegele

Reputation: 660

Construct your XML as a javascript variable and pass that variable as your data into your AJAX call.

var xmlFragment = '<complexType><simple attributeEx="attributeA">A</simple><simple attributeEx="attributeB">B</simple></complexType>';

See http://www.w3schools.com/xml/default.asp for more information on XML.

Upvotes: 0

Related Questions