locked
pass input from HTML page to web api RRS feed

  • Question

  • User-1471881183 posted

    hello all,

    i have two projects, one is pure web api, there i have my controlls and models, the same api is published in IIS. below is my sample api code to insert data

    second project has only html pages(UI Project). from html page using jquery i have collected input, now i have to send them into API published in IIS here, my doubts are

    Is is possible to send input to my API from HTML page itself using jQuery? if yes may i know 

    my web api seems below

    // POST: api/Employees
    [ResponseType(typeof(tblEmployee))]
    [Route("api/PosttblEmployee")]
    public IHttpActionResult PosttblEmployee(tblEmployee tblEmployee)
    {
    if (!ModelState.IsValid)
    {
    return BadRequest(ModelState);
    }
    
    db.tblEmployees.Add(tblEmployee);
    db.SaveChanges();
    
    return CreatedAtRoute("DefaultApi", new { id = tblEmployee.employeeId }, tblEmployee);
    }

    Monday, June 11, 2018 11:18 AM

All replies

  • User475983607 posted

    YEs, using jQuery AJAX.  This is rather mature subject and one that is easily found with a simple Google search.  This jQuery code will look similar to the code below.   However, you'll need to fill in the blanks and use jQuery selector to fetch the data from the HTML (the DOM) as we cannot see your code.

    var tblEmployee = {};
    tblEmployee.name = "Hello"
    $.ajax({
        method: "POST",
        url: "http://localhost:52441/api/PosttblEmployee",
        data: customer
    }).done(function (response) {
        console.log(response);
    });

    Also, spend some quality time reading the jQuery AJAX documentation.

    http://api.jquery.com/jquery.ajax/

    And the selector docs.

    https://api.jquery.com/category/selectors/

    If you are building a SPA then consult the docs for the framework you are using.

    Monday, June 11, 2018 11:34 AM