jquery Ajax Instance Application usage instructions

Source: Internet
Author: User
Tags html tags json php code

Often used to jquery+ajax/"target=" _blank >jquery Ajax, but sometimes forget how to write, take a note below. jquery Ajax Example:

The code is as follows Copy Code

Post method

$.ajax ({

URL: ' test.php ',

Type: ' Post ',

Data: {' act ': ' Addvideo ', ' videoname ': videoname},//uses JSON objects here

Success:function (data) {

Code here ...

},

Fail:function () {

Code here ...

}

});

Get mode

$.get ("test.php", {name: "John", Time: "2pm"}, function (data) {//code here ...});

Get mode 2

$.ajax ({

URL: ' test.php ',

Data: {' act ': ' Addvideo ', ' videoname ': videoname},//uses JSON objects here

Success:function (data) {

Code here ...

}

});

jquery Ajax Sync Way

$.ajax ({

URL: ' test.php ',

Type: ' Post ',

async:false,//How to use synchronization

Data: {' act ': ' Addvideo ', ' videoname ': videoname},//uses JSON objects here

Success:function (data) {

Code here ...

},

Fail:function () {

Code here ...

}

});


Instance

JQuery Ajax No Refresh form submission instance

The HTML code below, given the portability, does not write full HTML tags.

The code is as follows Copy Code

[HTML]
<?php
Header (' content-type:text/html;charset=utf-8′);
?>

<script type= "Text/javascript" src= "Http://code.jquery.com/jquery.min.js" ></script>
<script type= "Text/javascript" >
$ (function () {
$ ("#subbtn"). Click (function () {
var params = $ (' input '). Serialize ();
var url = "Your server-side PHP";

$.ajax ({
Type: "Post",
Url:url,
DataType: "JSON",
Data:params,
Success:function (msg) {
var Tishi = "Your submitted name is:" + Msg.name +
"<br/> The password you submitted is:" + Msg.password;
$ ("#tishi"). HTML (Tishi);
$ ("#tishi"). CSS ({color: "green"});
}
});
});

});

</script>
<p><label for= "name" > Name:</label>
<input id= "name" name= "name" type= "text"/>
</p>

<p><label for= "password" > Password:</label>
<input id= "password" name= "password" type= "password"/>
</p>

<span id= "Tishi" ></span>
<p><input id= "subbtn" type= "button" value= "Ajax test"/></p>
[/html]

The server PHP code is as follows

[PHP]
<?php

echo Json_encode ($_post);
[/php]

Ajax gets data examples via JSON

The code is as follows Copy Code


<script type= "Text/javascript" >
$ (document). Ready (function () {
Getscatalog ("Paidang", "M06″");

});

function Getscatalog (selectid,basecode) {
if (Basecode!= "") {
$.ajax ({
URL: "Ajax/getcatalogbybasecode.aspx",
Data: "code=" + encodeURI (basecode), Cache:false,
DataType: "HTML",
Success:function (context) {
Fillselect (Selectid, context);
}
});
}
else {
return "Error";
}
}

function Fillselect (Selectid, context) {
var listitem=new Array ();
ListItem = eval (context);
for (var i = 0; i < listitem.length; i++) {
$ ("#" + Selectid). Append ("<option value= '" + listitem[i]["code"] + "' >" + listitem[i]["name" + "</option>"); Append an option to select (dropdown

)
}
}

</script>

HTML code:

<select id= "Paidang" name= "Paidang" >
<option value= "" selected>== Please choose ==</option>
</select>

Ajax:

Create a new. aspx page Delete the HTML code in the. aspx page, add the following code in the. aspx.cs

The code is as follows Copy Code

string Rq_ Basecode=null;
Rq_basecode = request.querystring["code"];
if (string. Isnullorwhitespace (Rq_basecode))
{
Response.Write ("Error");
Response.End ();
}

Bllcatalog bll_info = new Bllcatalog ();
list<scatalog> lt_info = new list<scatalog> ();
Lt_info = Bll_info. GetCatalog (Rq_basecode, "");
//response.write (Rq_basecode);
if (lt_info. Count > 0)
{
Response.Write (Jsonhelper.tojson (Lt_info));
}
Else
{
Response.Write ("Null");
}

BLL layer of data:

Public list<m2model.scatalog> GetCatalog (string code, string Refcode)
{
Dalcatalog dalcatalog6 = new M2s Harpdal.dalcatalog ();
return DALCATALOG6. GetCatalog (code, Refcode);
}

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.