简体   繁体   中英

How to receive and save the data sent from client side (J-query using Ajax) to server side (ASP.net C#)?

I am trying to sent a JSON format from Client side JQuery using Ajax to ASP.net (C# code behind) and save the data. however i haven't been able to do so. I dont have much knowledge on how to handle client side request in Server-side.

Thanks in advance!

my code is as follow:

Client Side:

$.ajax(
        {
            type: "POST",
            url: "Default.aspx/save",
            data: "{'data': " + JSON.stringify(prj) + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
            if (response.ok) {
                prof.stop();
                if (response.data) {
                    ge.loadProject(response.data); 
                } else {
                    ge.reset();
                }
            } else {
                var errMsg = "Errors saving project\n\n";
                if (response.message) {
                    errMsg = errMsg + response.message + "\n";
                }

                if (response.errorMessages.length) {
                    errMsg += response.errorMessages.join("\n");
                }

                alert(errMsg);
            }
        }

    });

I'm not really familiar with handling JSON in code-behind so i have tried some approaches and I've got errors code behind in C# :

    [WebMethod]
    public static string save (object data)
    {

    //String s1 = data.ToString();
    //Dictionary<string, object> tmp = (Dictionary<string, object>)data;

    //DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(string));
    //MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(data));
    //string obj = (string)ser.ReadObject(ms);

    string s2 = data.ToString();
    GC.GClass g = new GC.GClass();
    g.Save(s2);
    return s2;
}

GC.GClass code to save the data as in a file:

namespace GC
 {
public class GClass
{
    string fileLoc = @"c:\Users\Pouria\Desktop\sample1.txt";



    public GClass()
    {

    }

    public void Save(string data)
    {
        FileStream fs = null;

        if (!File.Exists(fileLoc))
        {
            using (fs = File.Create(fileLoc))
            {
            }


            if (File.Exists(fileLoc))
            {
                using (StreamWriter sw = new StreamWriter(fileLoc))
                {
                    sw.Write(data);
                }
            }

        }
    }

}
}

and in my file in my response and my sample1.txt i get this as output:

 System.Collections.Generic.Dictionary`2[System.String,System.Object]

I couldn't upload images of FireBug but these is the response tab message:

 {"d":"System.Collections.Generic.Dictionary`2[System.String,System.Object]"}

and JSON tab:

 "System.Collections.Generic.Dictionary`2[System.String,System.Object]"

First thing is that, you have to create a WebService1.asmx file in project. After creating the file create a method in WebService1.asmx file.

[WebMethod]
public static string save (object data)
{

//String s1 = data.ToString();
//Dictionary<string, object> tmp = (Dictionary<string, object>)data;

//DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(string));
//MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(data));
//string obj = (string)ser.ReadObject(ms);

string s2 = data.ToString();
GC.GClass g = new GC.GClass();
g.Save(s2);
return s2;
}

Script

$.ajax(
    {
        type: "POST",
        url: "WebService1.asmx/save",
        data: "{'data': " + JSON.stringify(prj) + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
        if (response.ok) {
            prof.stop();
            if (response.data) {
                ge.loadProject(response.data); 
            } else {
                ge.reset();
            }
        } else {
            var errMsg = "Errors saving project\n\n";
            if (response.message) {
                errMsg = errMsg + response.message + "\n";
            }

            if (response.errorMessages.length) {
                errMsg += response.errorMessages.join("\n");
            }

            alert(errMsg);
        }
    }

});

Set a break point on Save method and run it. I think by using this you can solve your problem.

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