asp.net ajax 调用失败,返回 404

问题描述 投票:0回答:2

我正在尝试进行此 ajax 调用:

function Test(){
            var results = new Array();

            var emp1 = { "ID": "12", "Name": "Manas" };
            results.push(emp1);

            var emp2 = { "ID": "2", "Name": "Tester" };
            results.push(emp2);

            var postData = { empList: results };

            $.ajax({
                url: 'Sprachen/SaveEmpList',
                data: JSON.stringify(postData),
                type: 'POST',
                contentType: 'application/json',
                dataType: 'json',
                beforeSend: function () {
                },
                success: function (result) {
                    alert(result);
                },
                complete: function () {
                },
                failure: function (jqXHR, textStatus, errorThrown) {
                    alert("Status: " + jqXHR.status + "; Error: " + jqXHR.responseText); // Display error message
                }
            });
        }

我的控制器:

public static bool SaveEmpList(List<Employee> empList)
        {
            try
            {
                // Implement your logic to save EmpList in Database  
            }
            catch (Exception ex)
            {
                // Log Error  
                return false;
            }

            return true;
        }

这是我单击按钮调用 Test() 函数之前的基本 URL

https://localhost:44306/Jobs/Sprachen?categoryId=handwerk&handler=UserId

但是我收到了 404.

如何创建 URL 以便不再收到 404?

asp.net ajax
2个回答
0
投票

你的动作方法不应该是静态的,尽量去掉修饰符。另外,你的控制器叫

SprachenController
吗?如果没有,您将需要为您的操作添加
Route
HttpPost
属性,如下所示:

[HttpPost("Sprachen/SaveEmpList")]
public bool SaveEmpList(List<Employee> empList)
{
   ...
}

0
投票

这行得通:

$.ajax({
                type: 'POST',
                url: "/Jobs/Sprachen/?handler=RandomNumber",
                data: JSON.stringify({ Min: testvariable, Max: "no" }),
                contentType: "application/json",
                dataType: "json",
            }).done(function (data) {
                console.log(data);
            })
            .fail(function (error) {
                //nothing for now
            });
© www.soinside.com 2019 - 2024. All rights reserved.