js经过ajax给php发送数据:
保举:php效劳器
应用js数组来实现json工具的封装:
var arrX={resitems:[]}; arrX.bookid=bookid; for (var i=0; i < cellList.length; i++) { var item=cellList[i]; var jsonRes={}; jsonRes.bookid=bookid; jsonRes.res_id=item.itemStruct.id; jsonRes.res_name=item.itemStruct.name; jsonRes.src_origin=item.itemStruct.src; jsonRes.src_hd=item.itemStruct.src_hd; jsonRes.src_sd=item.itemStruct.src_sd; jsonRes.src_td=item.itemStruct.src_td; arrX.resitems.push(jsonRes); }; var jsonResStr=JSON.stringify(arrX);
下面的代码是先创立arrX的json工具,对工具增加各类属性、变量等。最初经过stringify转化为字符串,这个取得的字符串jsonResStr就是一个一般字符串了,能够经过ajax通报到效劳器了。
值患上留意的是,这里假如有中文,那是没成绩的,没有会呈现乱码成绩。正在实现了JOSN封装之后,开端应用ajax通报到PHP页面。
$.ajax({ type:"POST", url:"SQLHelper.php", dataType:"json", async:false, data:{'jsonResPanel':jsonResStr}, success:function(json){} });
此中type的类型能够是GET以及POST,url是效劳器解决的剧本顺序。dataType类型有text,json,xml等等,async通常应用false,data是详细要通报的json字符串,而且给效劳器post一个叫做json字段的数据,PHP端能够$_POST['json']就能够猎取post过去的数据。假如需求接纳前往数据。
以上就是js怎样经过ajax给php发送数据的具体内容,更多请存眷资源魔其它相干文章!
标签: php js ajax php教程 php故障解决 php使用问题
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!