如何在JSFiddle中使用localhost URL的getJSON返回键/值对?

时间:2016-06-15 14:22:14

标签: javascript json ajax cors jsfiddle

我已关注this tutorial使用Python,Flask,SQLite和SQLAlchemy构建API。我可以在浏览器中成功点击localhost地址。但是,是否可以测试此连接并在其他地方产生所需的结果,例如JSFiddle?

我想使用此网址http://localhost:5000/dept/police来检索所有工资键和值对

示例JSON

{
    "data": [{
        "department": "POLICE",
        "name": "AARON,  JEFFERY M",
        "position": "POLICE OFFICER",
        "salary": "$80778.00"
    }, {
        "department": "POLICE",
        "name": "AARON,  KARINA",
        "position": "POLICE OFFICER",
        "salary": "$80778.00"
    }, {
        "department": "POLICE",
        "name": "ABBATE,  TERRY M",
        "position": "POLICE OFFICER",
        "salary": "$86520.00"
    }, {
        "department": "POLICE",
        "name": "ABBOTT,  LYNISE M",
        "position": "CLERK III",
        "salary": "$43920.00"
    }, {
        "department": "POLICE",
        "name": "ABDALLAH,  ZAID",
        "position": "POLICE OFFICER",
        "salary": "$69684.00"
    }, {
        "department": "POLICE",
        "name": "ABDELHADI,  ABDALMAHD",
        "position": "POLICE OFFICER",
        "salary": "$80778.00"
    }]
}

1 个答案:

答案 0 :(得分:1)

是的,你可以。 JSFiddle在客户端运行,因此它可以访问locahost

但是你必须注意一些细节才能使它发挥作用。

  1. 必须在本地服务器上启用CORS。了解如何操作here
  2. 您无法通过https访问JSFiddle,如果您这样做,您的浏览器可能会因安全措施而阻止您的请求。阅读更多here
  3. 这是我制作的fiddle。它可以毫无问题地访问我的localhost