如果输入变量(a)未设置为Null,则以下Ajax代码甚至不会触发我的操作。Ajax为什么不发送数据?
Ajax代码:
var ab = "Chocolate Smoothies ya know?";
$("#customerSubmit").submit(function (e) {
e.preventDefault();
$.ajax({
url: "/api/Booking/lander",
method: "post",
data: { a: ab }
})
});
下面我控制器代码:
[HttpPost]
public void lander(string a)
{
System.Diagnostics.Debug.WriteLine(a);
}
当我不将它设置为空,接收到的输入为空。
截图时触发断点:
我用类型/方法/等似乎没有任何工作
更新:
我甚至尝试以下但没有用:
更新2:
即使以下没有工作,下列情况之一也给了我 - >“无法加载资源:服务器与405状态(不允许的方法)回应”
var ab = 'Chocolate Smoothies ya Know?';
$.ajax({
url:"/api/Booking/lander",
data: {'': ab}
});
随着
public string lander([FromUri]string asx) ////and asx = "" and /// asx = null
更新3
事情非常奇怪的事情,我用下面的代码:
,我得到了以下错误:
,当我用
////(string a = "")
它triggere我的行动出于某种未知的原因,发生了以下事情。
以下是我的WebApiConfig代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace HotelManagementSystem
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
//routes.MapHttpRoute("RestApiRoute", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" }); //this replaces your current api route
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
更新4:
即使下面设置没有工作:
它没有工作.... –
@derloopkat我看,所以你有什么建议? –
@derloopkat'@'使用(Html.BeginForm(“AddCustomer”,“api/Booking”,FormMethod.Post,new {id =“customerSubmit”})){} –