Flask provides cross-domain access to the JSON API, and AJAX receives JSON data

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.