【发布时间】:2014-11-28 09:04:51
【问题描述】:
我正在尝试实现一个包含 stringified(?) 对象的简单 jQuery ajax 帖子。我通过收集我打算在注册页面上发布的信息然后使用 JSON.stringify 并将对象存储在 cookie 中来实现这一点。注册成功后,该对象将发布在下一页的 ajax 帖子的 data 参数中。
到目前为止一切顺利。成功后,API 接收对象并解释数据。但是,当用户导航回该页面(包含他们的用户信息)并设置了 cookie 时,每次他们点击该页面时都会一次又一次地发送发布请求。
我想实现这样的目标:
if(CMjar.getKeys().indexOf('registerSession') != -1) { //if cookie exists
$.ajax({
url: 'API URL',
type: 'POST',
data: stringyObjS,
dataType: 'json',
}) // post the request
.done(function() { //once posted
CMjar.remove('registerSession'); //delete the cookie
});
console.log('posted');
}; // if cookie doesn't exist don't do anything
在控制台中工作时,CMjar.remove 函数按预期工作,cookie 消失了,但 .done 没有调用它。有任何想法吗?我应该使用成功吗:?
我还尝试了以下方法:
$.ajax({
url: 'API URL',
type: 'POST',
data: stringyObjS,
dataType: 'json',
}); // post the data
CMjar.remove('registerSession'); // delete the cookie
这在删除 cookie 的意义上是有效的,尽管当它应该发送 post 请求时我确实收到了一个语法错误(我没有复制这个,因为所以不能逐字引用错误)。
我在正确的轨道上吗?我是不是很傻?请帮忙!
谢谢, 迈尔斯
【问题讨论】: