【问题标题】:Web Api 2.0 working in postman but not in HTML pageWeb Api 2.0 在邮递员中工作但不在 HTML 页面中
【发布时间】:2021-03-30 23:31:29
【问题描述】:

这是我在 .Net 中用于 web api 2.0 的控制器代码 当我使用邮递员时,它工作正常,但是当我从 HTML 页面调用它时,我收到错误 No HTTP resource was found that matches the request URI 我做错了什么?

控制器代码

[HttpPost]
public IHttpActionResult GetCustomerName (string num)
{
   return Json(new
   {
      success = true,
      message = "My Name "+ num,
   });
}

HTML 代码

$.ajax({
   url: "http://localhost:10000/api/CFP/GetCustomerName",
   type: "POST",
   data: {
            'num': '123'
         },
   contentType: 'application/json; charset=utf-8',
   success: function (response) {
      alert(response.message);
   },
   error: function (xhr, error, thrown) {
      alert(xhr.responseText);
   }
});

错误截图

邮递员截图

【问题讨论】:

    标签: .net ajax postman asp.net-web-api2


    【解决方案1】:
    $.ajax({
       url: "http://localhost:10000/api/CFP/GetCustomerName",
       type: "POST",
       data: JSON.stringify({
                'num': '123'
             }),
       contentType: 'application/json; charset=utf-8',
       success: function (response) {
          alert(JSON.stringify(response));
       },
       error: function (xhr, error, thrown) {
          alert(xhr.responseText);
       }
    });
    

    你能不能试试上面的代码,把数据串起来

    您可以从邮递员本身创建正确的代码:

    只需点击代码并找到等效的 ajax 代码

    【讨论】:

      猜你喜欢
      • 2020-08-18
      • 1970-01-01
      • 2017-08-16
      • 2020-05-25
      • 2019-08-25
      • 2016-10-23
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多