0

I want to retrieve JSOn data from controller and set it for window.location.

in jquery :

 $('.starcontainer').rating(function (vote, event) {
        var el = $(event.target);
        post = el.parent().parent().attr("data-post");
        $(".loading").show();
        // we have vote and event variables now, lets send vote to server.
        var data = JSON.stringify({
            rate: vote, id: post
        });
        $.ajax({
            url: "/blog/rate",
            dataType: "Json",
            data: data,
            contentType: "application/json; charset=utf-8",
            type: "POST",
            success: function (responseData) {
                alert(responseData);
                window.location = responseData.redirect;
            }
        });

    });

and in Controller:

[AllowAnonymous]
    [HttpPost]
    public JsonResult Rate(int id, int rate)
    {
        if (Request.IsAuthenticated)
        {
            try
            {
                var user = _db.UserProfiles.SingleOrDefault(u => u.UserName == User.Identity.Name);
                PostScore score = new PostScore();
                score.BlogPostId = id;
                score.Score = rate;
                score.UserId = user.UserId;
                _db.PostScores.Add(score);
                _db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                // get last error
                if (ex.InnerException != null)
                    while (ex.InnerException != null)
                        ex = ex.InnerException;

            }
        }
        else
            return Json(new { redirect = Url.Action("Login", "Account") }, JsonRequestBehavior.AllowGet);

        return Json(new { redirect = Url.Action("Index") }, JsonRequestBehavior.AllowGet);
    }

When i run it, retrieve json data {"redirect":"/Account/Login"} for jquery. and redirect to http://localhost:2478/undefined (return undefined for redirect).

if i return Json(Url.Action("Login", "Account") , JsonRequestBehavior.AllowGet); it redirect to http://localhost:2478/%22/Account/Login%22

How to retrieve a url data from controller?

10
  • put it inside a variable and then return from json like: var url="/Account/Login" ..... return Json(new { redirect = url }, JsonRequestBehavior.AllowGet); Commented Aug 4, 2014 at 4:56
  • Thanks, i use a variable, but it is return undefined again :( Commented Aug 4, 2014 at 5:01
  • alert(responseData.redirect);...is saying undefined??? Commented Aug 4, 2014 at 5:05
  • It is return empty alert. Commented Aug 4, 2014 at 5:07
  • 1
    dataType: Json -> json? try it. Commented Aug 4, 2014 at 6:22

2 Answers 2

1

According to firebug console you are getting response as string so parse it to json as it is returned as string:

"{"redirect":"/Account/Login"}"

success: function (response) {
     var data = JSON.parse(response);    
     window.location.href = data.redirect;    
}
Sign up to request clarification or add additional context in comments.

Comments

1

The below code is working fine for me

return Json(new { Redirect = "/Account/Login" },JsonRequestBehavior.AllowGet);

and ajax success

success: function (response) {    
     window.location.href = response.Redirect ;    
}

1 Comment

it is return "{"redirect":"/Account/Login"}" for response and return undefined for response.Redirect

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.