Ajax Call在mvc中返回状态0

时间:2018-01-01 04:48:45

标签: c# ajax asp.net-mvc asp.net-ajax

我希望使用ajax将数据从我的视图发布到控制器。但是当我尝试使用下面的代码时,ajax调用总是触发错误,状态代码为0,而不是将数据传递给控制器​​。

如何修改我的代码以将数据传递给控制器​​。任何帮助都要提前感谢。

提交按钮代码

 <input class="btn btn-success" type="submit" value="Submit" onclick="GetData();" />                                                                                                                                              

Ajax Call

   function GetData() {
        $.ajax({           
        url: '@Url.Action("CaptureUserData","Home")',
        data: {
            IdentityNo: "1", FullName: "test",Dob:"12", Gender: "m", 
            PhoneNo: "000", PhoneNo1: "000",
            Email: "ooo", Category: "000", Password: "000"
        }, 
        method: 'post',
        dataType: 'json',
        async: false,
        success: function (response) {
            alert('Success');
        },
        error: function (xhr, status, error) {                
            alert(xhr.status);
        }
    });
}

控制器

  [HttpPost]
    public ActionResult CaptureUserData(string IdentityNo, string FullName, 
    string Dob,string Gender, string PhoneNo, string PhoneNo1,                                            string Email, string Category, string Password                                          )
    {

        return Json(new { IdentityNo = IdentityNo });
    }

2 个答案:

答案 0 :(得分:0)

const path = require('path');
const webpack = require('webpack');

module.exports = {
  entry: './src/index.js',
  output: {
  filename: 'bundle.js',
  path: path.resolve(__dirname, 'public/dist')
},
module: {
  rules: [
      {
          test: /\.js?$/, 
          loader: 'babel-loader',
          exclude: /node_modules/
      },
      {
          test: /\.css$/,
          use: [ 'style-loader', 'css-loader' ]
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        loader: "file-loader"
      }
      ,
      {
        test: /\.(png|jpeg|jpg|gif|svg)$/,
        loader: "file-loader"
      }
  ]
  },
  devServer: {
  contentBase: path.resolve(__dirname, "public"),
  historyApiFallback: true,
  port: 3000,
  watchOptions: {
    // Delay the rebuild after the first change
    aggregateTimeout: 300,

    // Poll using interval (in ms, accepts boolean too)
    poll: 1000,
  },
  },
  plugins: [
   // Ignore node_modules so CPU usage with poll
   // watching drops significantly.
   new webpack.WatchIgnorePlugin([
      path.join(__dirname, "node_modules")
   ])
 ],
 };

同时确认您是从同一域提出请求

答案 1 :(得分:0)

首先,您需要指定是否要使用Json发布数据,然后必须指定是否要使用Json返回数据。

示例(发布没有json stracture的数据并返回json结果): 您的控制器必须如下所示:

[HttpPost]
    public JsonResult CaptureUserData(string IdentityNo, string FullName, 
    string Dob,string Gender, string PhoneNo, string PhoneNo1,                                            string Email, string Category, string Password                                          )
    {
        return Json(IdentityNo);
    }

如您所见,返回类型必须是JsonResult而不是ActionResult。

ajax调用必须像这样:

function GetData() {
        $.ajax({           
        url: '@Url.Action("CaptureUserData","Home")',
        data: {
            IdentityNo: "1", FullName: "test",Dob:"12", Gender: "m", 
            PhoneNo: "000", PhoneNo1: "000",
            Email: "ooo", Category: "000", Password: "000"
        }, 
        method: 'post',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        success: function (response) {
            alert('Success');
        },
        error: function (xhr, status, error) {                
            alert(xhr.status);
        }
    });
}

您必须使用的内容类型为&application / x-www-form-urlencoded;字符集= UTF-8&#39;这是默认的。