我是靠谱客的博主 多情路人,最近开发中收集的这篇文章主要介绍python Flask 中ajax向后端发送数据的个人心得,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1 基础的样式

1.1 post
  $.ajax({
            url:'/user/news_release',
            type:'POST',
            data:JSON.stringify(params),
            contentType:'application/json',
            headers: {'X-CSRFToken': getCookie("csrf_token")},
            success:function (response) {
                if (response.errno==200){
                    alert('ok');
                    location.reload()
                }else{
                    alert(response.errmsg)
                }
            }
        })
get

这样写的话,参数需要拼接在url中

$.ajax({
        url:'/logout',
        type:'get',
        contentType: 'application/json',
        success:function (response) {
            if (response.errno == 200){
                alert(response.errmsg)
                location.reload()
            }else(
                alert('退出失败')
            )
        }
    })

这样写的话,可以将参数自动拼接在URL中

    $.ajax({
        url:'/newslist',
        type:'get',
        data:params,
        success:function (response) {
           
        }
    })

也可以使用简化的写法,参数也会拼接到url中

     $.get('/newslist',params,function (response) {
           data_querying = false;
           if (response.errno=200) {
           				pass
           }	
    })
}

若是在前端想向后端传输文件,例如照片,文件之类的,传统的ajax不可以传输图片,需要用到ajaxsubmit,这样ajax会自动将前端form表单里面有name属性的input,全部传到后端,后端也用name获取value

   $(this).ajaxSubmit({
            // 读取富文本编辑器里面的文本信息

            url: "/user/news_release",
            type: "POST",
            headers: {
                "X-CSRFToken": getCookie('csrf_token')
            },
            success: function (resp) {
                if (resp.errno == 200) {
                    // 选中索引为6的左边单菜单
                    window.parent.fnChangeMenu(6);
                    // 滚动到顶部
                    window.parent.scrollTo(0, 0)
                }else {
                    alert(resp.errmsg)
                }
            }
        })

最后

以上就是多情路人为你收集整理的python Flask 中ajax向后端发送数据的个人心得的全部内容,希望文章能够帮你解决python Flask 中ajax向后端发送数据的个人心得所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部