1

我想将 JSON 发布到控制器操作,并且我希望该操作将我引导到与 Ajax 响应不同的另一个视图:

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

控制器 :

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

1 回答 1

4

我很确定您不能通过正常请求发布 JSON。如果您担心重定向,那么我建议坚持使用 ajax 请求,并在成功函数中处理重定向。

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

和服务器代码

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

public ActionResult AnotherAction()
{
    return View();
}
于 2013-05-24T22:13:07.497 回答