Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从jquery ajax请求发送数据不起作用_Javascript_Jquery_Ajax - Fatal编程技术网

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

Javascript 从jquery ajax请求发送数据不起作用,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个从jquery ajax到ActionResult方法的post请求,如下所示: $("#itemTextbox, #itemTextboxNew, #quantity1Textbox").on("keydown", function (e) { if ((e.keyCode == 120){ var GetReportLast5SellAndBuyURL="/Trd/SellInvoice/GetReportLast5SellAndBuy"; itemCode = $(th

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

$("#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”为空。。。这一章怎么了

我尝试了不同的配方,但问题仍然存在。

$(“#itemTextbox,#itemTextboxNew,#quantity1texbox”)。打开(“按键”,函数(e){
如果((e.keyCode==120){
$.ajax({
类型:“POST”,
url:“/Trd/SellInvoice/GetReportLast5SellAndBuy?ItemCode=“+$(this).val(),
contentType:“应用程序/json”,
背景:这,,
数据类型:“JSON”,
processData:false
}).done(函数(msg){…某物…});}
//或
$(“#itemTextbox,#itemTextboxNew,#quantity1texbox”)。打开(“按键”,函数(e){
如果((e.keyCode==120){
$.ajax({
类型:“POST”,
url:“/Trd/SellInvoice/GetReportLast5SellAndBuy”,
contentType:“应用程序/json”,
数据:JSON.stringify({ItemCode:$(this.val()})
数据类型:“JSON”,
背景:这,,
processData:false
}).done(函数(msg){…某物…});}
试试这个:


只需在脚本中注释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 ...});}

在[

上解释得很好,你用引号试过了吗?比如
数据:{“ItemCode”:ItemCode}
是的..我试过了.你验证过ItemCode=$(this).val();返回了一些东西并且开始时不是空的吗?是的..它有值..我检查过我知道的最后一件事你试过数据:{'ItemCode='+ItemCode}?因为您正试图将其作为字符串传递给您的actionresult。您的建议是有效的,但不是好的解决方案。。thanks@Majid821你可以选择上面的一个。我给出了一个有效的解决方案。那就是谢谢你的回答。
dataType
选项用于指定响应的预期格式。它可以帮助我们如果省略,jQuery将解决此问题。它不会影响请求中的传出数据。您必须指定数据类型。另一件事是为什么您将上下文提到“this”。这是无效的。请尝试添加->contentType:“application/json;charset=utf-8”,然后它必须是您的URL。尝试在项目中更新不同的URL格式。是的..我删除了“context:this”并添加了contentType:“application/json;charset=utf-8”。因此它的值为真..谢谢
$("#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 ...});}