Django - Ajax: 在数据中发送 url 参数

Django - Ajax: sending url params in data

我正在尝试通过 Ajax 向 Django 服务器发送 "GET" 和 "POST" 请求。 首先,我提供 URLConf:

url(r'^write_to_file/(?P<file_name>.*)/(?P<content>.*)/$',generalFunctions.write_to_file, name ='write_to_file'),

现在,AJAX 部分。以前,我曾经这样做过(避免在数据中发送参数)

$.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) 
        }
    }); 

直到某个时刻,我对这种方法感到满意,但现在通过 "data" 变量传递 file_name 和内容对我来说很重要,由于某种原因我得到 404错误。

$.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) 
        }
    });

服务器端错误:

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

客户端错误:

jquery-2.1.1.min.js:4 GET http://127.0.0.1:8000/write_to_file/?file_name=my_file_name&content=my_content 404 (Not Found)

知道为什么吗? ajax 语法有问题还是与 URLConf 有某种关系?

url(r'^write_to_file/(?P<file_name>.*)/(?P<content>.*)/$',generalFunctions.write_to_file, name ='write_to_file'),

现在是错误的,您发送 post 请求的 url 是:/write_to_file/

url(r'^write_to_file/$',generalFunctions.write_to_file, name ='write_to_file'),

就是你想要的我觉得!