注:需先引入 jquery.json-xx.min.js
1. query传参
var name = '王一'; var age = 18; $.ajax({ type : 'get', url : "xxxxxx?name="+name+"&age="+age, async : false,//同步/异步 contentType : "application/x-www-form-urlencoded; charset=gbk", dataType : 'json', //返回 JSON 数据 beforeSend : function() { //调用前触发,如加载效果等 show('.load'); }, success : function(data, status) { var rstate = data.result; if (rstate == "0") { alert('接口调用成功!'); } else { alert('接口调用失败!'); } }, complete : function() { //调用后触发(不管成功或失败) hide('.load); }, error : function(data, status, e) { alert('接口调用错误!'); } });
2.param传参(传json数据)
var param = new Object(); param.name = '王一'; param.age = 18; $.ajax({ type : 'post',//也可为get url : 'xxxxx', async : false, contentType : "application/x-www-form-urlencoded; charset=gbk", data : { "param" : $.toJSON(param) //转换为json格式 }, dataType : 'json', beforeSend : function() { show('.load'); }, success : function(data, status) { var rstate = data.result; if (rstate == "0") { if (rstate == "0") { alert('接口调用成功!'); } else { alert('接口调用失败!'); } }, complete : function() { hide('.load); }, error : function(data, status, e) { alert('接口调用错误!'); } });
JavaScript调用API接口传参数方法
这篇博客介绍了两种在JavaScript中调用API接口并传递参数的方法:query传参和param传参,特别是param传参时如何传递JSON数据。文章强调在使用前需要引入jquery.json-xx.min.js库。
893

被折叠的 条评论
为什么被折叠?



