ASP. NET uses jquery JSON. Pay attention to the content returned by the background.

Source: Internet
Author: User

Background code

/// <Summary>
/// Summary of commonhandler
/// </Summary>
Public class commonhandler: ihttphandler
{

Public void processrequest (httpcontext context)
{
Context. response. expires =-1;
Context. response. contenttype = "text/plain ";
If (string. Equals (context. request ["ajaxmethod"]? ""), "Getroom", stringcomparison. ordinalignorecase ))
{
Roomselect (context );
}
Context. response. End ();
}

Public void roomselect (httpcontext context)
{
Httprequest request = context. request;
Httpresponse response = context. response;
String val = (request ["Val"]? ""). Trim ();
VaR list = businessroom_dal.businessroomlist.where (P => P. roomregion = Val );
Stringbuilder jsonbuilder = new stringbuilder ("{\" ID \ ": \" \ ", \" Val \ ": \" all \"},");
Foreach (businessroom model in List)
{
Jsonbuilder. appendformat ("{\" ID \ ": \" {0} \ ", \" Val \ ": \" {1} \ "}},", model. ID, model. room. replace ("\"",""));
}
String JSON = "[" + jsonbuilder. tostring (). trimend (',') + "]";
Response. Write (JSON );
}

Public bool isreusable
{
Get
{
Return false;
}
}
}

Front-end:

Function changeselect (OBJ, ddlroomid ){
OBJ = $ (OBJ );
Var val = obj. Val ();
OBJ. Next ('img '). Show ();
If (Val! = ""){
$. Ajax ({
Type: "Get ",
URL: '<% = resolveurl ("~ /Ashx/commonhandler. ashx ") %> ',
Data: 'ajaxmethod = getroom & val = '+ escape (VAL ),
Datatype: "JSON ",
Success: function (data ){
If (Data! = NULL ){
// Eval ("(" + RET + ")")
Alert (data [0]. DemoData );
}
OBJ. Next ('img '). Hide ();
},
Complete: function (){
OBJ. Next ('img '). Hide ();
},
Error: function (XMLHttpRequest, textstatus, errorthrown ){
// Usually in textstatus and errorthrown
// Only one containing information
// The options parameter passed when calling this Ajax request
}
});
}
}

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.