嗨,我需要一些幫助,我試圖用Ajax發送一些數據到控制器使用PUT方法做更新這裏是JavaScript和Ajax代碼:HttpPut與路由控制器
function UpdateProduct() {
var id = location.pathname.split('/')[2];
var product = {
Name: $("#txtName").val(),
Description: $("#txtDescription").val(),
Price: $("#txtPrice").val(),
SubCategoryId: $("#txtSubCategoryId").val(),
CompanyId: $("#txtSubCategoryId").val(
ProductCode: $("#ProductCode").val(
Barcode: $("#Barcode").val(
Brand: $("#txtBrand").val(),
Material: $("#txtMaterial").val(),
Date: $("#txtDate").val(),
};
$.ajax({
url: "/UpdateProduct/"+id,
type: "PUT",
dataType: "json",
data: product,
success: function (result) {
alert('Successful');
},
error: function (e) {
alert(e.error);
}
});
};
和我的控制器代碼:
[HttpPut]
[Route("UpdateProduct/{id}")]
public void UpdateProduct(int id, ProductsBE product)
{
var api = ConfigurationManager.AppSettings["apiUrl"];
var productsById = new ProductsRep();
var searchResponse = productsById.UpdateProduct(product,id, api);
}
的API工作,因爲我有一個郵遞員進行了測試。
好像你還沒有嘗試過,甚至因爲你已經給了我們幾乎沒有信息這調試自己....對於實例,當你執行javascript時,瀏覽器嘗試發送請求?瀏覽器請求的URL是什麼?是正確格式的請求的正文?等等。給我們一個瀏覽器用javascript發送請求的例子 - 並找出它與郵遞員請求中的請求有什麼不同。 – caesay
那麼你會得到空嗎? – Usman
阿賈克斯甚至沒有調用控制器它只警告錯誤消息...該網址是正確的,因爲我測試它也與此代碼:完整:function(){ alert(this.url) }, –