如何使用ajax post方法将复杂对象发送到c#controller?

时间:2016-07-15 19:17:30

标签: javascript c# asp.net-core-1.0

我试图将复杂的javascript对象发送到mvc控制器post方法。 每次运行该方法时,我都会收到BadRequest响应。它不允许我使用visual studio调试,只能使用Web开发人员工具。我和fiddler一起检查了我的JSON对象是否与View Model的顺序相同。有人可以帮我这个吗? 我使用的是Asp.Net Core 1.0。如果我需要提供更多信息,请告诉我。

这是我的观点模型:

public class RouteViewModel
    {
        public RouteViewModel() { }
        public List<CheckpointViewModel> Checkpoints { get; set; }
        public int TotalDistance { get; set; }
    } 
 public class CheckpointViewModel
    {
        public decimal Latitude { get; set; }
        public decimal Longitude { get; set; }
    }

编译对象的逻辑如下:

function createObject() {
    var routeModel = { Checkpoints: [] ,TotalDistance:totalDistance};
    for (var i = 0; i < markersOrders.length; i++) {
        var latlng = markersOrders[i].getPosition();
        var Checkpoint = {
            'Latitude': latlng.lat(),
            'Longitude': latlng.lng()
        };
        routeModel.Checkpoints.push(Checkpoint);
    }
    return JSON.stringify(routeModel);
}

这是我的ajax方法:

function saveRoute() {
    var apiUrl = location.origin + "/map/AddRoute";
    $.ajax({
        method: "POST",
        url: apiUrl,
        contentType: "application/json;charset=utf-8",
        data: (createObject())
    }).done(function (msg) {
        alert("Data saved: " + msg);
    }).error(function(msg){alert("Error: "+ msg)});
}

接收帖子的控制器如下:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult AddRoute([FromBody]RouteViewModel route)
        {
            RouteViewModel newRoute = route;
            if (ModelState.IsValid)
            {
                _context.AddRoute(newRoute.Checkpoints, newRoute.TotalDistance);
                _context.SaveRoute();
                return RedirectToAction("SavedRoutes");
            }
            else
            {
                return BadRequest(ModelState);
            }
        }

这是我的RAW帖子:

POST https://localhost:44343/map/AddRoute HTTP/1.1
Host: localhost:44343
Connection: keep-alive
Content-Length: 290
Accept: */*
Origin: https://localhost:44343
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: https://localhost:44343/Map/Map
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
Cookie: .AspNetCore.Antiforgery.4IsE6XVGxQo=CfDJ8HmVr1mwBStLrOG_4RXM-sje_SmjZYd-GboPG5E19rchqlC61XsNEwa7yOgIySC-U63iTH6cD0ggwqwmwdPJOibPKrxlctls_a_b3wmPvai80vYUx6j0Lckfn24GBf7X_xZhAl3eac892j7YDJWa9Oc; .AspNetCore.Identity.Application=CfDJ8HmVr1mwBStLrOG_4RXM-shK66Uthf3kfJJezCs7HCTztr-seJHxVj8l5MS66u4EWd72NEXUjebAIfHIxFZvHjZzjiQSVfLCxdHnmcsbYWXgGAmaA_sBjdimNQXnPAC-NMtp_fDeTCPJEoB1lBy1hl-GFQaAJdRzVrcc7OchWTSBVZ9jdHmm0htNyChcJ8BUCczH8FhVnPeFzlCM_reR8u2vsQrOxY_ZmczdUQ_mqCmTVLGDdRRJwHLhuafrZ2mmAXq1iDzQhprtv98qAx2zM4TSoAOBKoeALq_Oa2n1SDvFMMtGseDB1mLsj-LkPlKhcCmtB14kwDRctvOtxOqCbQTfFjhLlc5405_dccQjWJ3mITtn1ss3x1aHUP-pHHzFX9ZhusQ1-IqV4pPDs12c1q2B5Uz0qEOHaUByVEE5bKpzklTT2kxNW1V81aGMMmwbi9zFkuh9nUFnQmGCqf5VXSx-FTm-UDWZgyMnK0JpG7K4cpiSeycv9sOeP1qUlz-P28RXLhCvqYAX3FIccRfoQMf63tU5OfVhu1bhRdV_NQALhBpku9nrxFyxxECe5WRc4It-kCLiaOQBlYa9bewb80QiWIS-wHNDY5vVcdAkd2D5

{"Checkpoints":[{"Latitude":-34.004057732693184,"Longitude":25.649633891880512},{"Latitude":-34.00313273259371,"Longitude":25.65392542630434},{"Latitude":-34.001425013635725,"Longitude":25.653367526829243},{"Latitude":-34.00019756942711,"Longitude":25.650320537388325}],"TotalDistance":919}

正如@ Kiran Challa所建议的那样,我在控制器中收集了我的模型状态错误并将其写入Output窗口,如下所示:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult AddRoute([FromBody]RouteViewModel route)
        {
            RouteViewModel newRoute = route;
            if (ModelState.IsValid)
            {
                _context.AddRoute(newRoute.Checkpoints, newRoute.TotalDistance);
                _context.SaveRoute();
                return RedirectToAction("SavedRoutes");
            }
            else
            {
                //Getting errors
                var errors = ModelState.Values.SelectMany(v => v.Errors);
                Debug.WriteLine("Errors found: "+ errors+"\nEnd Errors found");
                return BadRequest(ModelState);
            }
        }

在我触发控制器的输出窗口中,我收到的是:

Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: The required antiforgery header value "RequestVerificationToken" is not present.
   at Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery.<ValidateRequestAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ValidateAntiforgeryTokenAuthorizationFilter.<OnAuthorizationAsync>d__3.MoveNext()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Warning: Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ValidateAntiforgeryTokenAuthorizationFilter'.
Microsoft.AspNetCore.Mvc.StatusCodeResult:Information: Executing HttpStatusCodeResult, setting HTTP status code 400
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 37.4837ms 400 

3 个答案:

答案 0 :(得分:1)

尝试让createObject()返回:

function createObject() {
    var routeModel = { Checkpoints: [] ,TotalDistance:totalDistance};
    for (var i = 0; i < markersOrders.length; i++) {
        var latlng = markersOrders[i].getPosition();
        var Checkpoint = {
            'Latitude': latlng.lat(),
            'Longitude': latlng.lng()
        };
        routeModel.Checkpoints.push(Checkpoint);
    }
    return JSON.stringify({route: routeModel});
}

答案 1 :(得分:0)

     var hash = [Int : [Int : [String : AnyObject]]] ()

    let number = hash[var1]?[var2]?["var3"]? .hashValue
    var d3 = [String : AnyObject]()
    var d2 =  [Int : [String : AnyObject]] ()
    d3["var3"] = var3  //var3 is a float

    if(number == nil)
    {
        d2[var2] = d3
        hash[var1] = d2

    }
    else
    {
        hash[var1]![var2] = d3

    }

worked out image

答案 2 :(得分:0)

我发现我哪里出错了。我要么需要发送所需的Verification Token和ajax post方法,要么在我的控制器中删除'[ValidateAntiForgeryToken]'。我选择了后者,因为安全不是我目前的目标。感谢@ KiranChalla -

的建议