C # Ajax returns JSON data--front and back interaction

Source: Internet
Author: User

I intern a piece, encountered this problem, on-line to find the test of the basic can not, oneself engaged a bit. For beginners To reference, if the great God, please advise.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Front-desk JavaScript code:

1<script>2         functionChecklogin () {3             varName = $ ("#name"). Val ();4             varPassward = $ ("#password"). Val ();5 Console.log (name);6 Console.log (passward);7 $.ajax ({8URL: ' Index.aspx?method=login ',9Type: ' GET ',TenData: {"name": Name, "Passward": Passward}, OneDataType: ' JSON ', ASuccessfunction(datainfo) { -                     if(datainfo.status) { - Console.log (datainfo.data); the                         //data in the foreground received is a string and needs to be converted to a JSON object -                         varJsondata =Json.parse (datainfo.data); -Alert ("Login successful, username is:" +jsondata.name+ "Sex is:" +jsondata.sex+ "Age is:" +jsondata.age); -}Else { +Alert ("Login Failed"); -                     } +                 }, AAsyncfalse at             }); -  -         } -</script>

Of course, after the success of data acquisition, how to deal with their own writing is good, here is just a demonstration.

Foreground HTML code:

1     <formID= "Form1"runat= "Server">2     <Div>3     <inputID= "Name"type= "text" />4     <inputID= "Password"type= "Password" />5     <Buttononclick= "Checklogin ()"value="">Submit</Button>6     </Div>7     </form>

Background CS code:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Web.UI;6 usingSystem.Web.UI.WebControls;7 usingNewtonsoft.json;8 usingSystem.Runtime.Serialization;9 Ten namespaceWebTest One { A      Public Partial classIndex:System.Web.UI.Page -     { -         protected voidPage_Load (Objectsender, EventArgs e) the         { -             stringmethod = request.querystring["Method"]; -             stringName = request.querystring["name"]; -             stringPassward = request.querystring["Passward"]; +             if(!string. IsNullOrEmpty (method)) -             { +                 if(Method = ="Login") A                 { at GetLogin (name, passward); -                 } -             } -  -         } -         Private voidGetLogin (stringNamestringPassward) in         { -              toCommonmodel msg =NewCommonmodel (); +             //There is no login successful can connect database judgment -             if(Name = ="Admin"&& Passward = ="Admin") the             { *                 //here is the information of the logged-on user that can be retrieved from the database . $                 varJsondata ="{\ "name\": \ "admin \", \ "sex\": \ "male \", \ "age\": \ "20\"}";Panax Notoginseng  -Msg.status =true; theMsg.msg ="Landing Success"; +Msg.data =Jsondata; A             } the             Else +             { -Msg.status =false; $Msg.msg ="failed"; $             } -             ObjectJsonobj =jsonconvert.serializeobject (msg); - Response.Write (jsonobj); the             //must be added, or the front end receive failed - Response.End ();Wuyi         } the     } -     classCommonmodel Wu     { -         //Status About         Private BOOL_statues; $  -          Public BOOLStatus -         { -             Get{return_statues;} A             Set{_statues =value;} +         } the         //message -         Private string_msg; $  the          Public stringmsg the         { the             Get{return_msg;} the             Set{_msg =value;} -         } in         //Data the         Private Object_data; the  About          Public ObjectData the         { the             Get{return_data;} the             Set{_data =value;} +         } -     } the}

C # Ajax returns JSON data--front and back interaction

Related Article

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.