1

I want to post JSON to controller action and I want that action to direct me to another view not same with Ajax response :

JS:

geocoder.geocode(
            {
                 'address': address
            },

            function (results, status) {
                if (status == google.maps.GeocoderStatus.OK)
                {
                    var o = { results: results };
                    var jsonT = JSON.stringify(o);
                    $.ajax({
                        url: '/Geocode/Index',
                        type: "POST",
                        dataType: 'json',
                        data: jsonT,
                        contentType: "application/json; charset=utf-8",
                        success: function (result) {
                            alert(result);
                        }
                    });
                }
        });

Controller :

 public ActionResult Index(GoogleGeoCodeResponse geoResponse)
 {
     string latitude = geoResponse.results[1].geometry.location.jb;
     string longitude = geoResponse.results[1].geometry.location.kb;
     ...
     return View("LatLong");
 }

1 Answer 1

4

I am pretty sure you cannot post JSON with a normal request. If your worry is the redirect then I suggest to stick with ajax request, and handle the redirect in the success function.

$.ajax({ type: "POST", data: { }, dataType: "json", url: "Home/AjaxAction" })
    .success(function (data) {
        window.location = data.redirectUrl;
    });

and server code

[HttpPost]
public JsonResult AjaxAction()
{
    // do processing
    return Json(new { redirectUrl = Url.Action("AnotherAction") });
}

public ActionResult AnotherAction()
{
    return View();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.