1

How to send json string via ajax and convert json string to xml?

Error: Failed to load resource: the server responded with a status of 500 (Internal Server Error)

$.ajax({
                async: true,
                url: "WebForm1.aspx/btnSave",
                type: "POST",
                data: "{data: '" + "{'?xml': {'@version': '1.0'},'Card': { 'Main_Client_Information': {'Surname': '','Name': '','Middle_name': '','Full_name': '','Short_name': '','RNN': '','IIN': '','Birthday': '','Doc_Type': {'@code': ''}}}}" + "'}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    alert(response.d);
                },
                error: function (error) {
                    debugger;
                    alert(error);
                }
            });

if send $.ajax data: '{data: "something"} - work perfect, how to put "json like string" instead "something"

WebForm.aspx.cs

[System.Web.Services.WebMethod]
    public static string btnSave(string data)
    {

        string response = "";

        try
        {

            XmlDocument xmlDocument = (XmlDocument)JsonConvert.DeserializeXmlNode(data);

            xmlDocument.Save(Server.MapPath("output.xml"));

            response = "success";

        }
        catch (Exception ex)
        {
            response = "error" + ex.Message;
        }

        return response;

    }

I just want get this string ---------> "{'?xml': {'@version': '1.0'},'Card': { 'Main_Client_Information': {'Surname': '','Name': '','Middle_name': '','Full_name': '','Short_name': '','RNN': '','IIN': '','Birthday': '','Doc_Type': {'@code': ''}}}}" + "'}" ------------ in webmethod btnSave and convert it to xml format

6
  • 1
    Have you debugged btnSave? because your exception is being thrown then. Commented Sep 11, 2015 at 4:56
  • data is not sended to server Commented Sep 11, 2015 at 5:05
  • does the answer here help? stackoverflow.com/questions/5092352/… Commented Sep 11, 2015 at 5:06
  • I don't think it's the cause of your problem, but your JSON string is missing a closing brace. It should end like {'@code': ''}}}}";. Commented Sep 11, 2015 at 5:09
  • Why send your data as a string? if you need to build your json as a string for some reason, you could always parse it client side using JSON.parse() (which is supported by most browsers) Commented Sep 11, 2015 at 6:41

2 Answers 2

0

the problem lies in the way you are telling jQuery which data to post :

You probably got confused, since the parameter passed on to $.ajax has a property named data. You are now passing a string right into there, while you should be passing a Json dictionary which contains which variable names and values you want to send.

try this :

Your entire call should look something like this :

(i'm keeping the data you are trying to send in a separate variable for clarity)

var stringData ="{'?xml' : '@version': '1.0'},'Card': { 'Main_Client_Information': {'Surname': '','Name': '','Middle_name': '','Full_name': '','Short_name': '','RNN': '','IIN': '','Birthday': '','Doc_Type': {'@code': ''}}}}";

$.ajax({
    async: true,
    url: "WebForm1.aspx/btnSave",
    type: "POST",
    data: {data:stringData},
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (response) {
        alert(response.d);
    },
    error: function (error) {
        debugger;
        alert(error);
    }
});  
Sign up to request clarification or add additional context in comments.

Comments

-1

You can't send the data as the json string. You need to call JSON.parse (json_string_here_).

It is also possie that you might need put instead of post, but i cant be sure of that because i dont know if you are doing an insert or update.

Sorry, even after that ive got to say that id be a little more than impressed if you can send an xml file like that. I would imagine that doesnt work very well.

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.