AJAX:将数据POST到不同的url
AJAX: POST data to a different url
我正在创建一个事件注册页面。
在此页面上,用户需要能够搜索数据库项目,以便将其注册到此事件。因此,POST请求必须提交到与用户所在的URL不同的URL,这样它就不会干扰表单向导序列。
这是我的javascript:
(document).ready(function() {
$('#primary_artist_lookup').on('input', function(){
console.log("Listener success");
var searchItem = $('#primary_artist_lookup').val();
$.ajax({
url : $('#lookupLink').val(),
type : "POST", //http method
data : searchItem,
dataType: "json",
// handle a successful response
success : function (json) {
console.log(json); // log the returned json to the console
console.log("success"); // another sanity check
},
// handle a non-successful response
error : function(xhr,errmsg,err) {
$('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
" <a href='#' class='close'>×</a></div>"); // add the error to the dom
console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
}
});
});
url是一个隐藏的html值,它映射到我试图将数据POST到的url:
<p hidden id="lookupLink">{% url "Users:lookup" %}</p>
为了安全起见,还有一个额外的js部分可以向Django提交CSRF令牌:
// CSRF VALIDATION //
// This function gets cookie with a given name
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;
}
var csrftoken = getCookie('csrftoken');
/*
The functions below will create a header with csrftoken
*/
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
function sameOrigin(url) {
// test that a given url is a same-origin 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));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
// Send the token to same-origin, relative URLs only.
// Send the token only if the method warrants CSRF protection
// Using the CSRFToken value acquired earlier
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
return false;
});
当我尝试使用这个ajax函数时,它不会发布到我请求的url,而是发布到用户所在的页面。我做错了什么?
改用text()
:
url : $('#lookupLink').text(),
数据应该是一个对象:
data : { searchItem : searchItem },
相关文章:
- JavaScript 中的 POST url 被注释掉了
- 使用 POST 方法通过 ajax 发送的数据在发送到服务器之前是否必须进行 URL 编码
- Angular 2:HTTP Post Request,带有URL参数和正文类型参数
- 使用表单和POST方法通过Javascript获取URL参数
- 如何在不更改URL的情况下使用HTTP方法POST
- qoxdoo:POST方法,不在URL中显示参数
- 如何在NodeJS中对POST请求进行url解码
- jQuery/PHP-POST-Forbidden(403)当试图在我的JSON数据中发送完整的URL时
- 当使用预先签名的POST url将文件上传到AWS S3时,设置随机文件名
- 获取链接帖子使用脚本“data:post.url”博主不起作用
- jQuery $.ajax post getting referer url for error debugging
- XMLHttpRequest POST 请求到当前 URL 的有效 URL 字符串
- AngularJS在使用method:POST时$resource调用错误的API URL
- 使用Joomla的jRoute库时POST URL的问题
- 如何在 ASPX 页的代码隐藏中使用 POST 设置图像的 url
- Ajax Post请求总是运行错误功能,尽管卷曲到相同的url工作正常
- Ajax 无法使用 jquery url post 方法
- 从javascript到php的动态url POST
- Node.js URL POST rewrite
- Ajax POST调用不能正常工作,返回错误代码0,即使url POST成功