在服务器端,我只是将json作为字典打印到控制台

@app.route('/',methods=['GET','POST'])
@login_required
def index():
    if request.method == "POST":
        print request.json.keys()
    return "hello world"

现在,每当我通过ajax发出post请求时,控制台就会打印出包含我需要的内容的字典.

On the client-side, I have been trying to use various methods to execute some jquery based on a successfull ajax call. I just realized that this might be an error on my server-side, i.e I am not sending any request header to tell jquery that its ajax call was a success.

So how do I send an OK status back to my client to tell it everything is all right?

为了完整起见,以下是我的客户端代码

$.ajax({
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify(myData),
    dataType: 'json',
    url: '/',
    success: function () {
        console.log("This is never getting printed!!")
    }});

推荐答案

About Responses瓶:

About Responses

The return value from a view function is automatically converted into a response object for you. If the return value is a string it's converted into a response object with the string as response body, a 200 OK status code and a text/html mimetype. The logic that Flask applies to converting return values into response objects is as follows:

  1. 如果返回了正确类型的响应对象,则直接从视图返回.
  2. If it's a string, a response object is created with that data and the default parameters.
  3. 如果返回一个元组,元组中的项可以提供额外的信息.这样的元组必须是(response, status, headers)(response, headers)形式,其中至少有一项必须在元组中.status值将覆盖状态代码,headers可以是附加标题值的列表或字典.
  4. 如果这些都不起作用,则Flask将假定返回值是有效的WSGI应用程序,并将其转换为响应对象.

So, if you return text string (as you are doing), the status code that your AJAX call has to receive is 200 OK, and your success callback must be executing. However, I recommend you to return a JSON formatted response like:

return json.dumps({'success':True}), 200, {'ContentType':'application/json'} 

Jquery相关问答推荐

JQuery日期 Select 器未设置从jQuery中 Select 的月份起90天或3个月

jQuery从JSON创建嵌套列表

如果您的 Select 器对象无效,为什么 jQuery 不会炸弹?

bootstrap 模式中的自动完成问题

jQuery绑定到粘贴事件,如何获取粘贴的内容

延期与promise

Select 除第一个之外的所有子元素

在 jquery 中启用/禁用下拉框

测试空 jQuery Select 结果

在jquery中查找具有某个属性值的所有元素

jQuery、复选框和 .is(":checked")

如何在 JavaScript 中的对象数组中查找值?

jQuery ajax 成功回调函数定义

CSS3 相当于 jQuery slideUp 和 slideDown?

无法更新数据属性值

如何在一个元素上拥有多个数据绑定属性?

使用 jQuery 将事件侦听器添加到动态添加的元素

使用中键触发 onclick 事件

catch forEach 最后一次迭代

Bootstrap:在 Modal 中打开另一个 Modal