gpt4 book ai didi

jquery - django 将 "post"识别为 "get"

转载 作者:太空宇宙 更新时间:2023-11-04 10:54:32 25 4
gpt4 key购买 nike

我所有的“post” Action 都被识别为“get” 我已经尝试了我所知道的一切来解决这个问题,但一切似乎都井井有条。提交表单每次都返回“GET”。

网址.py

from django.conf.urls.defaults import *
urlpatterns = patterns('',
url(r'^buildit/$', 'main.apps.builder.views.main'),
)

View .py

from django.http import HttpResponse

def main(request):
return HttpResponse(request.method)

html 表单

<form id="myform">
<input type="checkbox" name="list" value="audio"/> Audio<br />
<input type="checkbox" name="list" value="video"/> Video<br />
<input type="submit" value="Get Custom Library!" />
</form>

j查询

$(document).ready(function() {

$("#myform").submit(function() {



serialize = $(this).serialize()

$.ajax({
type: 'POST',
url: '/django/builder/buildit',
data: serialize,
crossDomain: false,
success: function(response){
alert(response);
}
});

return false;

$(document).ajaxSend(function(event, xhr, settings) {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
function sameOrigin(url) {
// url could be relative or scheme relative or absolute
var host = document.location.host; // host + port
var protocol = document.location.protocol;
var sr_origin = '//' + host;
var origin = protocol + sr_origin;
// Allow absolute or scheme relative URLs to same origin
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
// or any other URL that isn't scheme relative or absolute i.e relative.
!(/^(\/\/|http:|https:).*/.test(url));
}
function safeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}

if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
});

});
});

最佳答案

现在我不是 Django 的专家...但是如果出现 Append_Slash 问题,Django 似乎会覆盖任何请求。它将在末尾附加斜线重定向请求,此时,它将丢失任何 POST 信息并返回 GET 方法。也许尝试将/放在/django/builder/buildit 的末尾?在黑暗中拍摄...(似乎这仅在 APPEND_SLASH = false 时才重要...)

关于jquery - django 将 "post"识别为 "get",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11026257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com