简体   繁体   English

正确的jQuery语法,用于执行带标头的$ .post(ASP.NET MVC)

[英]Correct jQuery syntax for doing a $.post with a header (ASP.NET MVC)

I'm working on a ASP.NET MVC project that uses some jQuery on the client side. 我正在一个ASP.NET MVC项目上,该项目在客户端使用一些jQuery。 I have a jQuery call like this, which works correctly: 我有一个像这样的jQuery调用,它可以正常工作:

$.post($('form').attr("action"), $('form').serialize(), function(data){
    // Deal with the data that came back from the ASP.NET MVC controller
}

I want to do the exact same call, except I also want to send in a custom header. 除了要发送自定义标头之外,我还想进行完全相同的调用。 I am able to successfully create and send a custom header like this: 我能够成功创建并发送如下自定义标头:

var token = $('input[name="__RequestVerificationToken"]').val();
var headers = {};
headers['__RequestVerificationToken'] = token;

$.ajax({
    url: "/report_observation/" + submitType, 
    cache: false,
    type: "POST",
    data: {
    'viewModel': $('form').serialize(),
    },
    headers: headers,
    success: function (data) {
    // Deal with the data that came back from the ASP.NET MVC controller
    },
    error: function (response) {
    alert("Error: " + response);
    }
});

There are two problems with this second bit of code. 第二段代码有两个问题。 One is that I have to make a custom url to go to the correct controller, and I don't know of a way to simply use $('form').attr("action") to automatically go to the correct place. 一种是我必须创建一个自定义URL才能转到正确的控制器,而且我不知道一种简单地使用$('form').attr("action")自动转到正确位置的方法。

The second -- and bigger -- problem is that I'm not able to pass over the form data with $('form').serialize() as I could in the one liner $.post example. 第二个也是更大的问题是,我无法像在一个衬里$.post示例中那样使用$('form').serialize()传递表单数据。 Doing a @Html.Raw(Json.Encode(Model)) in my Razor cshtml file doesn't send over the model for the whole form, presumably because this code is within a partial view that doesn't know the state of the models in the other partial views that make up this form. 在我的Razor cshtml文件中执行@Html.Raw(Json.Encode(Model))不会传递整个表单的模型,大概是因为此代码在不知道模型状态的局部视图中在构成此表单的其他局部视图中。

Anyway, I'm thinking there must be an easy way to take this code... 无论如何,我认为必须有一种简单的方法来获取此代码...

var token = $('input[name="__RequestVerificationToken"]').val();
var headers = {};
headers['__RequestVerificationToken'] = token;

...and incorporate the headers property into this bit of code: ...并将headers属性合并到以下代码中:

$.post($('form').attr("action"), $('form').serialize(), function(data){
    // Deal with the data that came back from the ASP.NET MVC controller
}

However, I can't figure out the correct syntax. 但是,我找不到正确的语法。 Any ideas? 有任何想法吗?

OK, I figured it out. 好,我知道了。 The second example can indeed work if the data field looks like this: 如果数据字段如下所示,则第二个示例确实可以工作:

data:  $("form").serialize(),

In other words, I had to NOT assign it to the viewModel parameter. 换句话说,我不必将其分配给viewModel参数。

yes, for the second problem use that dev5000 says, and, for the URL, simply get the value for the attribute action fo the form and use it: 是的,对于第二个问题,使用dev5000所说的,对于URL,只需获取表单的属性操作的值并使用它:

var url = $("from").attr("action");
$.ajax({
    url: url, 
...
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM