「GET」および「POST」リクエストを Ajax 経由で Django サーバーに送信しようとしています。まず、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 と関係があるのでしょうか?