【发布时间】:2014-12-09 14:48:31
【问题描述】:
数据保留在表中,但在该页面未重定向到指定目标之后。
可以通过ajax重定向吗?
请检查代码并帮助我
function foodLog(){
var getDate = $('#ddBox').val();
var stringDate = getDate;
var newdate = stringDate.split("/").reverse().join("-");
var data={
servings : $('#demo_vertical').val(),
calories : "12",
carbs : $('#carbs').text(),
carter : $('#carter').text(),
cholestral : $('#cholestral').text(),
date: newdate,
fiber : $('#fiber').text(),
foodItem: $('#foodItem').text(),
foodtype:$('#foodtype').text(),
totalFat : $('#totalFat').text(),
saturatedFat : $('#satFat').text(),
sugar : $('#sugar').text(),
protein: $('#protein').text(),
size :$('#size').text(),
};
$.ajax({
type : "POST",
contentType: "application/json; charset=utf-8",
url : "/fitbase/foodlog/create",
dataType: "text",
data : JSON.stringify(data),
success : function(response) {
if (response.redirect){
console.log("Redirecting to page")
window.location.href = '/';
}
},
error : function(e) {
alert("Object" +e);
}
});
};
【问题讨论】:
-
console.log出现了吗?我的意思是response.redirect是真的吗? -
@PlantTheIdea 不,它没有打印
-
试试:
window.location = '/'; -
由于您指定
dataType: "text",response将是一个字符串,而不是一个对象。所以response.redirect没有意义。 -
这意味着您没有收到回复。真正的问题不是“为什么不重定向”,而是“为什么我没有得到预期的响应”。尝试做
console.log(response)看看你得到了什么对象。编辑:没有注意到你有dataType:'text',是的,那会搞砸的。