0

我正在尝试通过 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) 
        }
    }); 

直到某个时刻,我对这种方法感到满意,但现在通过“数据”变量传递文件名和内容对我来说很重要,由于某种原因,我得到了 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 有某种关系?

4

1 回答 1

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

现在是错误的,您发送帖子请求的网址是:/write_to_file/

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

是你想要的我想!

于 2016-04-15T11:14:47.810 回答