url = "/IPM/KPI/Add"; input = new FormData($('#frmKPI').get(0)) ;
PostAjax: function (isForm, url, input, divErrID, btnID, isSuccessFunction, isFailedFunction, NoModalAlert, isAsync)
{
var contentType = isForm ? false : "application/json;";
var processData = isForm ? false : true;
$(btnID).prop("disabled", true);
Loading(true);
$.ajax({
url: url,
type: "POST",
data: input,
async: isAsync == null || isAsync == undefined ? false : isAsync,
dataType: "json",
headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
contentType: contentType,
processData: processData,
success: function (data) {
if (data.IsSuccess) {
if (isSuccessFunction != null)
isSuccessFunction(data);
if (!NoModalAlert)
ModalAlert(MODAL_HEADER, data.Result);
$(divErrID).html("");
} else {
var msg = "";
if (data.IsListResult == true) {
for (var i = 0; i < data.Result.length; i++) {
msg += data.Result[i] + "<br />";
}
} else {
msg += data.Result;
}
ModalAlert(MODAL_HEADER, msg);
if (isFailedFunction != null)
isFailedFunction();
}
$(btnID).prop("disabled", false);
Loading(false);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("AJAX Error:", jqXHR.status, jqXHR.statusText);
console.log("Response Text:", jqXHR.responseText);
if (isFailedFunction != null)
isFailedFunction();
Loading(false);
$(btnID).prop("disabled", false);
ModalAlert(MODAL_HEADER, jqXHR.responseText);
},
});
}
请帮助,我尝试将数据转换为字符串化,但它不起作用。基本上我正在使用 .net core razor web-api,并且我正在尝试在保存表单时发布表单,并且应该转到我的 Add.cshtml.cs (handler = OnPostAsync)
如果要调用
Add.cshtml.cs
中的webapi方法,我们需要修改URL,例如https://your_sitename.com/Add?handler=methodname
。
我的测试代码在Index.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace WebApplication2.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
public IActionResult OnGetApi()
{
var data = new { Message = "Hello from API" };
return new JsonResult(data);
}
public IActionResult OnPostAApi()
{
var data = new { Message = "Hello from API" };
return new JsonResult(data);
}
}
}
Get Request测试结果(方法名为Api)
Post请求测试结果(方法名称为AApi)