ajax 提交post get 数据,得到json结果解析

前端之家收集整理的这篇文章主要介绍了ajax 提交post get 数据,得到json结果解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

ajax 请求接口,并处理结果

  1. //id为 btnBuyBox
  2. $('#btnBuyBox').click( function(){
  3. //$("#formjs").submit();
  4. $.ajax({
  5. //提交数据的类型 POST GET
  6. type : "POST",//提交的网址
  7. url : "/carSnatch/buyInin",//提交的数据
  8. data: {
  9. ID: ${commInfo.id},number: $("#carnumber").html(),costmoney:$("#costmoney").html()
  10. },//返回数据的格式
  11. datatype : "json",//"xml","html","script","json","jsonp","text".
  12. //在请求之前调用函数
  13. //beforeSend : function() { $("#msg").html("logining");},
  14. //成功返回之后调用函数
  15. success : function(data) {
  16. //$("#msg").html(decodeURI(data)
  17. var dataJson = JSON.stringify(data);
  18. var jsonInfo = JSON.parse(dataJson);
  19. //以后有特殊条件直接添加
  20. if(jsonInfo.flog == -1){
  21. alert(jsonInfo.msg);
  22. }else if(jsonInfo.flog == 0){
  23. alert(jsonInfo.msg);
  24. }else if(jsonInfo.flog == 1){
  25. alert(jsonInfo.msg);
  26. }
  27. },//调用执行后调用函数
  28. complete : function(XMLHttpRequest,textStatus) {
  29. //HideLoading();
  30. },//调用出错执行的函数
  31. error : function() {
  32. //请求出错处理
  33. }
  34. });
  35.  
  36. });

jquery 请求

  1. $.getJSON("http://sanic.cnblogs.com/",{param:"sanic"},function(data){
  2. //此处返回的data已经是json对象
  3. //1.这里解析的是平常的list 数组模式的
  4. $.each(data.root,function(i,item){
  5. if(i==0){
  6. return true;//同countinue,返回false同break
  7. }
  8.  
  9. //清空输入的数字框的值
  10. $("#info").html("");//清空info内容
  11. //解析显示
  12. //item.name item.age item.text
  13.  
  14. });
  15.  
  16. //2.如果是 map模式的json
  17. //jquery解析map数据
  18. $.each(data.infomap,function(key,value){
  19. alert(key+"----"+value);
  20. });
  21.  
  22. });

猜你在找的Ajax相关文章