javascript - How to write the data part better when interacting with the backend using ajax?
PHP中文网
PHP中文网 2017-06-30 09:58:14
0
2
734

Now when using ajax to interact with the backend, in the data part of ajax, the method used is to bind the jq nodes to the output parameters one by one (as shown in the figure). This method can handle it when there are few parameters, but when there are too many parameters, both coding and maintenance efficiency will become lower. When writing the data part of ajax, how can we optimize it better?

$.ajax({ url: "/openapi/test.json", dataType: "json", type: "post", formTypeData: true, data: { "keywords": $(".fmc-plate").attr("data-actioncontent"), "openId": localOpenId, "msgType": Msg.msgTypeElem, 'msgBody': Msg.msgBodyElem, "enable": true, "type": 3, "replyType": 3 }, success: function (json) { data = json.data; if (json.success == false) { noty({ text: "保存失败,请检查是否填入所有所需数据", layout: "top", timeout: 3000, type: "error" }); return false; } else { noty({ text: "保存成功", layout: "top", timeout: 1000, type: "success" }); $(".fmc-plate").remove(); $noty.close(); } } });
PHP中文网
PHP中文网

认证高级PHP讲师

reply all (2)
ringa_lee
将data 为一个json数据 form 表单数据使用序列化 $('#form1').serialize(); 节点数据可以先通过对象转化为json数据传到后台
一维数组 var data = {} data['id'] = 1; data['type'] = 2; 二维数组 var data = {}; var child1 = data[1] = {}; var child2 = data[2] = {}; child1['id'] = 1; child1['type'] = 2; child2['id'] = 2; ... 上面的数据还是对象,不能直接使用 转为 json JSON.stringify(data)
    淡淡烟草味

    It’s easy to use jquery, just use serialize()

    html

    js

    $('#form1').serialize();
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!