如果輸入變量(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」})){} –