jquery ajax实例应用使用说明

作者:袖梨 2022-11-14

经常用到jquery+ajax/">jquery ajax,但有时候会忘记怎么写,下面做个笔记。 jquery ajax例子:

代码如下 复制代码

//post 方式

$.ajax({

url : 'test.php',

type : 'post',

data : {'act':'addvideo', 'videoname':videoname},//这里使用json对象

success : function(data){

//code here...

},

fail:function(){

//code here...

}

});

//get 方式

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

//get 方式2

$.ajax({

url : 'test.php',

data : {'act':'addvideo', 'videoname':videoname},//这里使用json对象

success : function(data){

//code here...

}

});

//jquery ajax同步方式

$.ajax({

url : 'test.php',

type : 'post',

async: false,//使用同步的方式

data : {'act':'addvideo', 'videoname':videoname},//这里使用json对象

success : function(data){

//code here...

},

fail:function(){

//code here...

}

});


实例

jQuery Ajax 无刷新表单提交实例

HTML 代码如下,鉴于可移植性,没有写全 HTML 的标记。

代码如下 复制代码

[html]
header(‘Content-Type:text/html;charset=utf-8′);
?>


$.ajax({
type: “post”,
url: url,
dataType: “json”,
data: params,
success: function(msg){
var tishi = “您提交的姓名为:” + msg.name +

您提交的密码为:” + msg.password;
$(“#tishi”).html(tishi);
$(“#tishi”).css({color: “green”});
}
});
});

});








[/html]

服务器 PHP 代码如下

[php]

echo json_encode($_POST);
[/php]

ajax通过json获取数据例子

代码如下 复制代码


});

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 $(“#” + selectid).append(“”); //为Select追加一个Option(下拉项

)
}
}

html代码:

Ajax:

新建一个.aspx页面删除.aspx页面里的html代码删除,在.aspx.cs里添加如下代码

代码如下 复制代码

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 lt_info = new List();
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层的数据:

public List GetCatalog(string code, string refcode)
{
DALCataLog dalcatalog6 = new M2SharpDAL.DALCataLog();
return dalcatalog6.GetCatalog(code, refcode);
}

相关文章

精彩推荐