2

Data is persisting in table but after that page is not redirected to the specified destination.

Is redirection through ajax possible?

Please check the code and help me out

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);
        }  
    });  
};
Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

0

I think window.location.href should contain the entire redirection url instead of '/' in your code.

DoubleDotM
  • 61
  • 4