mishap
mishap

Reputation: 8505

Post JSON to asp.net MVC controller, not Ajax

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");
 }

Upvotes: 1

Views: 3672

Answers (1)

alexb
alexb

Reputation: 971

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();
}

Upvotes: 4

Related Questions