Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to receive and save the data sent from client side (J-query using Ajax) to server side (ASP.net C#)?
    primarykey
    data
    text
    <p>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. </p> <p>Thanks in advance!</p> <p>my code is as follow:</p> <p>Client Side:</p> <pre><code>$.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); } } }); </code></pre> <p>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# : </p> <pre><code> [WebMethod] public static string save (object data) { //String s1 = data.ToString(); //Dictionary&lt;string, object&gt; tmp = (Dictionary&lt;string, object&gt;)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; } </code></pre> <p>GC.GClass code to save the data as in a file:</p> <pre><code>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); } } } } } } </code></pre> <p>and in my file in my response and my sample1.txt i get this as output:</p> <pre><code> System.Collections.Generic.Dictionary`2[System.String,System.Object] </code></pre> <p>I couldn't upload images of FireBug but these is the response tab message:</p> <pre><code> {"d":"System.Collections.Generic.Dictionary`2[System.String,System.Object]"} </code></pre> <p>and JSON tab:</p> <pre><code> "System.Collections.Generic.Dictionary`2[System.String,System.Object]" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload