【问题标题】:Ajax redirect not performed [duplicate]未执行 Ajax 重定向[重复]
【发布时间】: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',是的,那会搞砸的。

标签: jquery ajax


【解决方案1】:

我认为 window.location.href 应该在您的代码中包含整个重定向 url 而不是“/”。

【讨论】:

  • 您可以在那里使用相对 URL。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多