2

This is my Javascript code that gets the value from the query string

function getUrlVars() {
        var name = [], hash;
        var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < url.length; i++) {
            hash = url[i].split('=');
            name.push(hash[0]);
            name[hash[0]] = hash[1];

            var hid =hash[1];
            return pid;
        }
    }

    var hid = getUrlVars()['pid'];

This is my URL: http://localhost:33454/Product?pid=1

[HttpGet]
    [Route("api/product/getproducts/{pid}")]
    public IActionResult Product(int pid)
    {

        var selectProducts= (from p in _db.Products
                           where p.ProductId == pid
                           select p).ToList();

        return Ok(selectProducts);
    }

This is my AJAX for printing the list to the html

    var $view = $('.table')
    $(function () {
        $.ajax({
            type: 'GET',
            url: '/api/product/getproducts/{pid}',
            datatype: 'json',
            success: function (products) {
                $.each(products, function (i, product) {
                    $view.append('<td class="products">' + product.productName + '</td>', '<td class="products">' + product.productDescription + '</td>');
                });
            }
        });
    });

Are there any issues with my code. Sorry, I know this might be a really simple question to you all. I'm new to programming. Thanks in advance.

0

2 Answers 2

1

The API URL is not being constructed properly for the AJAX call.

//...code omitted for brevity

var hid = getUrlVars()['pid'];

//...

var $view = $('.table');
$(function () {
    $.ajax({
        type: 'GET',
        url: '/api/product/getproducts/' + hid, //<<<<
        datatype: 'json',
        success: function (products) {
            $.each(products, function (i, product) {
                $view.append('<td class="products">' + product.productName + '</td>', '<td class="products">' + product.productDescription + '</td>');
            });
        }
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Nkosi, so for my controller that I included the [Route] attribute is not necessary right?
@vinxnk The route is necessary. That route template is what allows the controller action to be called. It defines what the URL should look like. That is what determined that the URL you were calling was incorrect because it did not match the template defined on the controller. Reference Routing to controller actions in ASP.NET Core
0

Change your code to this.

[HttpPost]
public IActionResult Product(int pid)
{

    var selectProducts= (from p in _db.Products
                       where p.ProductId == pid
                       select p).ToList();

    return Json(selectProducts, JsonRequestBehavior.AllowGet);
}

And this should be:
Replace {ControllerName} with your controller's name with no curly braces
For example: url: '/Clients/Details'

var $view = $('.table')
$(function () {
    $.ajax({
        type: 'POST',
        url: '/{ControllerName}/Product',
        data: JSON.stringify({ pid: hid}),
        dataType: 'json',
        success: function (products) {
            $.each(products, function (i, product) {
                $view.append('<td class="products">' + product.productName + '</td>', '<td class="products">' + product.productDescription + '</td>');
            });
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.