繁体   English   中英

dot net core 3.1 web API的POST方法在ajax调用中不起作用

[英]POST method of dot net core 3.1 web API not working in ajax call

$.ajax({
            url: Url,
            type: "POST",
             async: false,
            crossDomain: true,
            dataType: 'jsonp',
            contentType: "application/json",            
            data: {
                'cAmount': '2000',
                'lAmount': '20'
            },
            success: function (data) {
                if (data.responseStatus == "Success") {
                    alert(result.successData.outputData.amt)
                    if (result.successData.outputData.amt== 'Success')
                        toastr.success('Amounts updated Successfully...!');
                    else
                        toastr.success('Something went wrong while updating amount details...!');

                }
                else {
                    toastr.error('Something went wrong');
                }
            }
        });

我在 dot net core3.1 web api 中从 ajax 调用 post 方法。 它不工作

我在启动页面添加了这个

services.AddCors(c => { c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin()); });

在配置方法中

app.UseCors(options => options.AllowAnyOrigin());

以上代码适用于 ajax GET 方法,不适用于 ajax POST 方法。

您能否更新我的问题的任何答案。

它与cors无关。 修复 ajax

$.ajax({
            url: Url,
            type: "POST",
            data: {
                'cAmount': '2000',
                'lAmount': '20'
            },
            ....

你必须为 post action 创建视图模型

public class ViewModel
{
 public int CAmount {get;set}
 public int LAmount {get;set}
}

和行动

public IActionResult MyAction(ViewModel model)
{
}

暂无
暂无

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

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