locked
Web Api 2.0 working in postman but not in HTML page RRS feed

  • Question

  • User851533892 posted

    Here's my controller code for Web API 2.0 in .Net.
    When I use postman then it works fine but when I call it from a HTML page then I get following error. What am I doing wrong?

    No HTTP resource was found that matches the request URI

    Controller Code

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

    HTML Code

    $.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);
       }
    });

    WebApiConfig

    public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services
    
                // Web API routes
                config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            }

    Error Screenshot


    Postman Screenshot

    Monday, December 21, 2020 8:53 AM

All replies

  • User1120430333 posted

    Why don't  you put the exact  uri you usedmin postman into the program and see that it works. If it does work, the you'll know the uri may be malformed in some manner that is causing the error.

    Monday, December 21, 2020 9:45 AM
  • User851533892 posted

    Web Api is listening on HTTP port 10000 as evident in Postman screenshot and I am using same URL in my HTML page.

    Monday, December 21, 2020 9:54 AM
  • User1120430333 posted

    I changed my first reply.

    Monday, December 21, 2020 9:56 AM
  • User1120430333 posted

    Your uri is malformed the one you are using in the program. Where is the '?' Indicating that parm data is coming on the uri?

    Monday, December 21, 2020 10:04 AM
  • User1120430333 posted

    The uri you are using on the post in the program needs to look exactly like the one you used for postmon.

    Monday, December 21, 2020 10:10 AM
  • User1120430333 posted

    One last thing, you can get rid of 'data:' in the ajax post and use the uri you used for postman.

    Monday, December 21, 2020 10:20 AM
  • User851533892 posted

    It seems this is happening due to CORS not enabled.

    My Web API is hosted on HTTP while the HTML page from where I am calling it is on HTTPS. But I wonder how come Postman is able to access it?

    Monday, December 21, 2020 10:28 AM
  • User303363814 posted

    Postman Uri (from screenshot)     = http://localhost:10000/api/CFP/GetCustomerName?num=123

    Wepage Uri (from error message) = http://localhost:10000/api/CFP/GetCustomerName

    These are not the same.

    Monday, December 21, 2020 11:21 AM
  • User-474980206 posted

    Postman is passing the data via query string (url), but your Ajax is passing via the post body.

    Monday, December 21, 2020 3:47 PM