0

I'm trying to figure out how to send a complex object through javascript to my C# webservice.

Here xml string:

<?xml version="1.0" encoding=utf-8?><soap:Envelope xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>
<SaveResponse xmlns="http://tempuri.org/">
  <RL>
    <response_entries>
      <ResponseEntry>
        <response_id>string</response_id>
        <account_id>string</account_id>
        <organization_id>string</organization_id>
        <form_header_id>string</form_header_id>
        <status>string</status>
        <field0>string</field0>
        <field1>string</field1>
        <field99>string</field99>
      </ResponseEntry>
      <ResponseEntry>
        <response_id>string</response_id>
        <account_id>string</account_id>
        <organization_id>string</organization_id>
        <form_header_id>string</form_header_id>
        <status>string</status>
        <field0>string</field0>
        <field1>string</field1>
        <field99>string</field99>
      </ResponseEntry>
    </response_entries>        
  </RL>
</SaveResponse>

Example of what I'm trying to send:

enter image description here

8
  • This is barely readably, why not just paste the text? Commented Mar 18, 2011 at 21:27
  • 4
    Do you control the server? I'd be writing this as a json service, not SOAP... and if I don't, I think I'd write a json service that acts as a proxy to the SOAP service ;p Commented Mar 18, 2011 at 21:27
  • @Marc Yes. I have control over the server. I have not much experience with JSON. I did create a JSOn service one time. Ex. [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public String GetFormByFormHeaderIDJSON(String form_header_id) { FormFunctions FF = new FormFunctions(); return new JavaScriptSerializer().Serialize(FF.GetFormByFormHeaderID(form_header_id)); } Is this what you mean? Still confused on calling the web service from the client side? Have an example I can work with? Commented Mar 18, 2011 at 21:33
  • @Hitesh well, it doesn't even need to be a [MebMethod] - any handler that takes a json body and runs it through JavaScriptSerializer should work fine. Commented Mar 18, 2011 at 21:39
  • @Hitesh Rana, shame on you for including a bunch of code in a comment and not even putting it in a code block. Commented Mar 18, 2011 at 21:42

1 Answer 1

3

As Marc mentioned, it is very easy to use JSON in javascript, so lets do that:

First create a web service:

[WebMethod]
public string DoStuff(string json)
{
    var js = new JavaScriptSerializer();
    MyType input = js.Deserialize<MyType>(json);
    return js.Serialize(DoStuff(input));
}

That input is a json string which JavaScriptSerializer will turn into a .NET object very easily. DoStuff(MyType) is a method you define that takes whatever input you need and does stuff with it.

Now it's time to consume (I'm using GET here, but if it's active you should POST):

$.get("TestService.asmx/DoStuff", { json: jsonString })
    .success(function (data) { 
        // code goes here!
    });

use json2.js to serialize the jsonString from a javascript object

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.