Ajax determines whether the returned data is JSON when the request is successful, response Content-type is set to Application/json, and the data is in the standard JSON format
jquery version
<script src= "Https://cdn.bootcss.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" ></script>
Flask code Example
@app. Route ('/test ', methods=[' GET ', ' POST ') def test (): a = [] a.append (Newobject (). __dict__) # Set Content-type resp=response (Json.dumps (a), mimetype= ' Application/json ') #支持跨域请求 resp.headers[' Access-control-allow-origin '] = ' * ' return resp
Ajax example, the request returns a result, a JSON-converted Object
$.ajax ({url: "Http://192.168.11.130/test", type: ' GET ', DataType: ' JSON ', Success:function (Result) {Console.log ("Ajax Succeed") Console.log (typeof (R Esult)) Obj=result; var pcapdata= '; for (Var i=0;i<obj.length;i++) {pcapdata=pcapdata+ ' <tr> ' pcapdata=pcapdata+ ' <td> ' +obj[i].time+ ' </td> '; pcapdata=pcapdata+ ' <td> ' +obj[i].srcip+ ' </td> '; pcapdata=pcapdata+ ' <td> ' +obj[i].dstip+ ' </td> '; pcapdata=pcapdata+ ' <td> ' +obj[i].httphost+ ' </td> '; pcapdata=pcapdata+ ' <td> ' +obj[i].requesturi+ ' </td> '; pcapdata=pcapdata+ ' <td> ' +obj[i].pcapfile+ ' </td> ' + ' </tr> '; } document.getElementById ("TD1"). Innerhtml=pcapdata; } })
Flask provides cross-domain access to the JSON API, and AJAX receives JSON data