Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON - Slashes and application type..refactoring
    text
    copied!<p>My code works (yeah!) which sends json to a server.. would appreciate any thoughts on refactoring</p> <p>1) My C# code sends this json to the server</p> <p>{\"firstName\":\"Bill\",\"lastName\":\"Gates\",\"email\":\"asdf@hotmail.com\",\"deviceUUID\":\"abcdefghijklmnopqrstuvwxyz\"}</p> <p>Which I have to get rid of the slashes on the server side....not good.</p> <p>2) I'm using application/x-www-form-urlencoded and probably want to be using application/json</p> <pre><code>Person p = new Person(); p.firstName = "Bill"; p.lastName = "Gates"; p.email = "asdf@hotmail.com"; p.deviceUUID = "abcdefghijklmnopqrstuvwxyz"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUri + "newuser.php"); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; //TODO request.ContentType = "application/json"; JavaScriptSerializer serializer = new JavaScriptSerializer(); string s = serializer.Serialize(p); textBox3.Text = s; string postData = "json=" + HttpUtility.UrlEncode(s); byte[] byteArray = Encoding.ASCII.GetBytes(postData); request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close (); WebResponse response = request.GetResponse(); //textBox4.Text = (((HttpWebResponse)response).StatusDescription); dataStream = response.GetResponseStream (); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd (); textBox4.Text += responseFromServer; reader.Close (); dataStream.Close (); response.Close (); </code></pre> <p>PHP Code on Server:</p> <pre><code>$inbound = $_POST['json']; // this strips out the \ $stripped = stripslashes($inbound); $json_object = json_decode($stripped); echo $json_object-&gt;{'firstName'}; echo $json_object-&gt;{'lastName'}; echo $json_object-&gt;{'email'}; echo $json_object-&gt;{'deviceUUID'}; </code></pre>
 

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