2017-03-07 8 views
0

でこんにちは、私は私がここで更新を行うにはPUTメソッドを使用して、コントローラにAjaxでいくつかのデータを送信しようとしているいくつかの助けが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が働いています。

+0

あなたのオブジェクトを変換するためにJSON.stringifyを使用する必要がありますあなたがjavascriptを実行すると、ブラウザは要求を送信しようとしましたか?ブラウザはどのURLを要求しましたか?リクエストのポストボディが正しい形式であったか?ブラウザがjavascriptで送信する要求の例を与え、郵便配達要求などからの要求とどのように異なるのかを調べてください。 – caesay

+0

あなたはnullになっていますか? – Usman

+0

ajaxはコントローラにエラーメッセージだけを警告することさえしません...私はこのコードでもこれをテストしているため、URLは正しいです:complete:function(){ alert(this.url) }、 –

答えて

0

あなたがのために....私たちのほとんど無い情報を与えてくれたので、あなたもこれを自分でデバッグしようとしていないように思えproductjsonに文字列

$.ajax({ 
    url: "/UpdateProduct/"+id, 
    type: "PUT", 
    dataType: "json", 
    data: JSON.stringify(product), 
    success: function (result) { 
     alert('Successful'); 
    }, 
    error: function (e) { 
     alert(e.error); 
    } 
}); 
+0

まだ動作していません –

+0

f12とネットワークタブで応答を確認できますか – Usman

関連する問題