95CACA 2018-03-18 07:35 采纳率: 33.3%
浏览 1433
已采纳

django +jquery+ ajax get提交报错,post提交就可以

 模板:(index.html)
“
<html>
<body>
<p>请输入两个数字</p>
<form action="/index" method="get">
    a: <input type="text" id="a" name="a"> <br>
    b: <input type="text" id="b" name="b"> <br>
    <p>result: <span id='result'></span></p>
    <button type="button" id='sum'>提交</button>
</form>

<!---<script type="text/javascript" src="/static/jquery-3.3.1.min.js"></script>--->
<script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
      $("#sum").click(function(){
        var a = $("#a").val();
        var b = $("#b").val();

        $.get("/index",{'a':a,'b':b}, function(ret){
            $('#result').html(ret.result)
        })
      });
    });
</script>
</body>
</html>”
**url 代码:**
urlpatterns = [
    url(r'testDb' , testDb),
    url(r'query' , Query),
    url(r'formdemo' , searchtest.search),
    url(r'^seachDemo' , searchtest.search_form),
    url(r'^index' , view.loginIndex),
]

 python 代码view.py:

def loginIndex(request):
    if(request.method == 'GET'):
        a = int(request.GET.get('a'))
        b = int(request.GET.get('b'))
        return_json = {'result': a + b}
        return HttpResponse(json.dumps(return_json), content_type='application/json')
    return  render(request,"index.html")
    报错:
    TypeError at /index

int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Request Method: GET
Request URL: http://127.0.0.1:8000/index
Django Version: 2.0.3
Exception Type: TypeError
Exception Value:

int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Exception Location: D:\PythonDemo\PythonDemo\view.py in loginIndex, line 12
Python Executable: D:\PythonDemo\venv\Scripts\python.exe
Python Version: 3.6.3
Python Path:

['D:\PythonDemo',
'D:\PythonDemo',
'D:\PythonDemo\venv\Scripts\python36.zip',
'X:\python\DLLs',
'X:\python\lib',
'X:\python',
'D:\PythonDemo\venv',
'D:\PythonDemo\venv\lib\site-packages',
'D:\PythonDemo\venv\lib\site-packages\setuptools-28.8.0-py3.6.egg',
'D:\PythonDemo\venv\lib\site-packages\pip-9.0.1-py3.6.egg',
'X:\PyCharm\PyCharm 2017.3.4\helpers\pycharm_matplotlib_backend']

Server time: Sun, 18 Mar 2018 07:35:40 +0000

这个报错也让我很懵逼,我把提交方式,和ajax方法改成post,就没有这个报错,程序可以正常运行

  • 写回答

2条回答 默认 最新

  • 三阶魔方 2018-03-18 09:31
    关注

    这句你应该看得懂,告诉你了,参数需要是string或者数字,但不能是空。
    int() argument must be a string, a bytes-like object or a number, not 'NoneType'
    明显你的request.GET.get('a')是空值。
    你个$.get方法也对的。
    你的代码也是对的。
    那么只有$.get下的data节点有可能出错了。
    你改为:{a:a,b:b}试一试。
    同时,使用Chrome或者firefox去看network里面那条产生的请求对不对。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能