"Go" jquery submits data to the server in JSON form

Source: Internet
Author: User

jquery encapsulates Ajax data requests, making the operation much easier to implement. In the past, we had to write a lot of code to implement this function, now we just need to call the $.ajax () method and indicate the way, address, data type, callback method, etc. of the request. The following code shows how to encapsulate the client form data in JSON format and then send the data to the server through jquery's Ajax request, and eventually store the data in the database. The server is defined as a. ashx file, in fact you can define the server as any type that can receive and process client data, such as the Web Service,asp.net page,handler.

First, on the client side, the page form data is encapsulated in JSON format through a JavaScript script. The Getjsondata () function completes this function. We then send the data to the REQUESTDATA.ASHX on the server through the $.ajax () method. It uses the Json.stringify () method, which can convert the data sent by the client into a JSON object, the details of which can be seen here https://developer.mozilla.org/en-US/docs/Web/ Javascript/reference/global_objects/json/stringify

$ ("#btnSend"). Click (function () {
$ ("#request-process-patent"). HTML ("Submitting data, do not close the current window ...");
$.ajax ({
Type: "POST",
URL: "Requestdata.ashx",
ContentType: "Application/json; Charset=utf-8 ",
Data:JSON.stringify (Getjsondata ()),
DataType: "JSON",
Success:function (message) {
if (Message > 0) {
Alert ("The request has been submitted! We will get in touch with you as soon as possible ");
}
},
Error:function (message) {
$ ("#request-process-patent"). HTML ("Submit data failed!") ");
}
});
});

function Getjsondata () {
var json = {
"ClassID": 2,
"Name": $ ("#tb_name"). Val (),
"Zlclass": "Test Type 1, test Type 2, test Type 3",
"PName": $ ("#tb_contact_people"). Val (),
"Tel": $ ("#tb_contact_phone"). Val ()
};
return JSON;
}

Take a look at the server code, REQUESTDATA.ASHX.

[Serializable]
public class Requestdatajson
{
public int ClassID {get; set;}
public string name {get; set;}
public string Zlclass {get; set;}
public string PName {get; set;}
public string Tel {get; set;}
}

<summary>
Summary description for RequestData
</summary>
public class Requestdata:ihttphandler
{
public void ProcessRequest (HttpContext context)
{

Context. Response.ContentType = "Application/json";
var data = context. Request;
var sr = new StreamReader (data. InputStream);
var stream = Sr. ReadToEnd ();
var javascriptserializer = new JavaScriptSerializer ();
Encapsulates the submitted data into the entity object studententity
var posteddata = javascriptserializer.deserialize<studententity> (stream);


int sid= Posteddata.sid;
String name = Posteddata.sname;

Context. Response.ContentType = "Text/plain";

}

public bool IsReusable
{
Get
{
return false;
}
}
}

"Go" jquery submits data to the server in JSON form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.