前端之家收集整理的这篇文章主要介绍了
ajax请求生成表单,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
前端HTML:
<div id="condition">
<label class="condition">条件</label><input type="text" name="condition" class="equipment_sz">
<input type="submit" value="IP查找" class="equipment_search_btn es_1">
ajax请求:
node2:/django/mysite/static/Js/Equipment#cat index.js
$(function(){
var _ip_bt = $(".content .con #condition .es_1");
var _env_bt = $(".content .con #condition .es_2");
_ip_bt.on('click',function(){
var _condition = $(".equipment_sz").val();
var data={};
data.condition=_condition;
var successfn = function(jdata){
console.log(jdata);
console.log(jdata[0]);
$(".gridtable").empty();
var _HTML="<tr><th>IP </th><th>Info</th><th>ENV</th></tr>"+"<tr><td>"+jdata[0]+"</td><td>"+jdata[1]+"</td><td>"+jdata[2]+"</td></tr>";
$(".gridtable").html(_HTML);
};
var errorfn = function(){
alert("数据获取出错");
};
$.ax(
"/api/search_ip/",data,successfn,errorfn
);
});
接口:
def search_ip(req):
print req.get_full_path()
print req.POST['condition']
ip=req.POST['condition']
conn= MysqLdb.connect(
host='192.168.137.3',port = 3306,user='root',passwd='1234567',db ='DEVOPS'
)
cur = conn.cursor()
a = cur.execute("select ip,info,env from machine_info where ip=%s ",[ip])
info = cur.fetchone()
print info
print type(info)
return HttpResponse(json.dumps(info),content_type="application/json")
原文链接:https://www.f2er.com/ajax/160490.html