Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Java脚本:使用AJAX调用Flask API失败_Python_Ajax_Flask - Fatal编程技术网

Python Java脚本:使用AJAX调用Flask API失败

Python Java脚本:使用AJAX调用Flask API失败,python,ajax,flask,Python,Ajax,Flask,我正在JS中尝试一个简单的FlaskAPI,但失败了。我使用curl测试了这个URL,结果很好。不知道我哪里做错了。非常感谢你的帮助 http响应代码很好(200)。可能是我写js的方式有问题 python脚本: from flask import Flask import simplejson as json app = Flask(__name__) @app.route('/api/person', methods=['GET']) def get_person(): str

我正在JS中尝试一个简单的FlaskAPI,但失败了。我使用curl测试了这个URL,结果很好。不知道我哪里做错了。非常感谢你的帮助

http响应代码很好(200)。可能是我写js的方式有问题

python脚本:

from flask import Flask
import simplejson as json

app = Flask(__name__)

@app.route('/api/person', methods=['GET'])
def get_person():
    str = {'id':'1','first_name':'John','last_name':'Smith'}
    res = json.dumps(str)
    return res

if __name__ == '__main__':
    app.run(debug=True)
testapp.js:

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "http://localhost:5000/api/person",
        success: function(data){
           $('.person-id').append(data.id);
           $('.person-first_name').append(data.first_name);
           $('.person-status').append('success');
        },
        error: function(){
           $('.person-status').append('failed');
        }

    });
});
index.html:

<!DOCTYPE html>
<html>
    <head>
        <title>TestApp jQuery</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script src="testapp.js"></script>
    </head>

    <body>
        <div>
            <p class="person-status">The Status is </p>
            <p class="person-id">The ID is </p>
            <p class="person-first_name">First name is </p>
        </div>
    </body>
</html>

TestApp jQuery

状态为

该id为

名字是


您的api似乎返回了一个stringify json

尝试:


你的浏览器控制台怎么说?你能告诉我们错误回调的错误吗?忘了说,你在哪里运行你的网站,例如,哪个端口?可能是因为跨国公司的问题,看起来跨国公司才是问题所在。我使用端口5000运行它的localhost。我把它改为XMLHttpRequest,它工作得很好。
$(document).ready(function() {
    $.ajax({
    type: "GET",
    url: "http://localhost:5000/api/person",
    success: function(data){
       data=JSON.parse(data)
       $('.person-id').append(data.id);
       $('.person-first_name').append(data.first_name);
       $('.person-status').append('success');
    },
    error: function(){
       $('.person-status').append('failed');
    }
    });
});
$(document).ready(function() {
$.ajax({
    type: "GET",
    dataType:"json",
    url: "http://localhost:5000/api/person",
    success: function(data){
       $('.person-id').append(data.id);
       $('.person-first_name').append(data.first_name);
       $('.person-status').append('success');
    },
    error: function(){
       $('.person-status').append('failed');
    }
});
});