繁体   English   中英

从jquery ajax请求发送数据不起作用

[英]Send data from jquery ajax request not working

我有一个从jquery ajax到ActionResult方法的帖子请求,如下所示:

$("#itemTextbox, #itemTextboxNew, #quantity1Textbox").on("keydown", function (e) {
if ((e.keyCode == 120){
   var GetReportLast5SellAndBuyURL="/Trd/SellInvoice/GetReportLast5SellAndBuy";
   itemCode = $(this).val();
   $.ajax({
        type: "POST",
        url: GetReportLast5SellAndBuyURL,
        data: {ItemCode:itemCode},
        //contentType: "application/json; charset=utf-8",
        context: this,
        processData: false
    }).done(function (msg) { ... somethings ...});}

在控制器中,ActionResult是:

    [HttpPost]
    public ActionResult GetReportLast5SellAndBuy(string ItemCode)
    { ... somthings ...}

但是当调用ActionResult时,“ItemCode”为空......本章有什么问题?

我尝试了不同形式的这个食谱,但问题仍然存在..

尝试这个:

 $.ajax({
        type: "POST",
        url: GetReportLast5SellAndBuyURL,
        data: JSON.stringify({ItemCode:itemCode}),
        datatype: "JSON",
        contentType: "application/json; charset=utf-8",
        processData: false
    }).done(function (msg) { ... somethings ...});}

只需在脚本中注释processData:false即可

$("#itemTextbox, #itemTextboxNew, #quantity1Textbox").on("keydown", function (e) {
if ((e.keyCode == 120){
   var GetReportLast5SellAndBuyURL="/Trd/SellInvoice/GetReportLast5SellAndBuy";
   itemCode = $(this).val();
   $.ajax({
        type: "POST",
        url: GetReportLast5SellAndBuyURL,
        data: {ItemCode:itemCode},
        //contentType: "application/json; charset=utf-8",
        context: this
       // processData: false
    }).done(function (msg) { ... somethings ...});}

很好地解释了[ 在jQuery中将processData设置为false会破坏我的AJAX请求

 $("#itemTextbox, #itemTextboxNew, #quantity1Textbox").on("keydown", function (e) { if ((e.keyCode == 120){ $.ajax({ type: "POST", url: "/Trd/SellInvoice/GetReportLast5SellAndBuy?ItemCode="+$(this).val(), contentType: "application/json", context: this, datatype: "JSON", processData: false }).done(function (msg) { ... somethings ...});} // OR $("#itemTextbox, #itemTextboxNew, #quantity1Textbox").on("keydown", function (e) { if ((e.keyCode == 120){ $.ajax({ type: "POST", url: "/Trd/SellInvoice/GetReportLast5SellAndBuy", contentType: "application/json", data:JSON.stringify({ItemCode:$(this).val()}) datatype: "JSON", context: this, processData: false }).done(function (msg) { ... somethings ...});} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM