Django - Ajax: sending url params in data
我正在尝试通过 Ajax 向 Django 服务器发送 "GET" 和 "POST" 请求。
首先,我提供 URLConf:
1 | url(r'^write_to_file/(?P<file_name>.*)/(?P<content>.*)/$',generalFunctions.write_to_file, name ='write_to_file'), |
现在,AJAX 部分。以前,我曾经这样做过(避免在数据中发送参数)
1 2 3 4 5 6 7 8 9 10 11 | $.ajax({ type:"GET", url: '/write_to_file/' + file_name + '/' + content , data: {}, success: function(data){ alert ('OK'); }, error: function(){ alert("Could not write to server file" + file_name) } }); |
直到某个时刻,我对这种方法感到满意,但现在通过"数据"变量传递文件名和内容对我来说很重要,但由于某种原因,我得到了 404 错误。
1 2 3 4 5 6 7 8 9 10 11 12 | $.ajax({ type:"GET", url: '/write_to_file/', data: {'file_name':file_name, 'content':content}, success: function(data){ alert ('OK'); }, error: function(){ alert("Could not write to server file" + file_name) } }); |
服务器端错误:
1 2 | Not Found: /write_to_file/ [15/Apr/2016 14:03:21]"GET /write_to_file/?file_name=my_file_name&content=my_content HTTP/1.1" 404 6662 |
客户端错误:
任何想法为什么? ajax 语法有问题还是与 URLConf 有某种关系?
1 | url(r'^write_to_file/(?P<file_name>.*)/(?P<content>.*)/$',generalFunctions.write_to_file, name ='write_to_file'), |
现在是错误的,您发送帖子请求的 url 是:/write_to_file/
1 | url(r'^write_to_file/$',generalFunctions.write_to_file, name ='write_to_file'), |
是你想要的我想!