skip to Main Content

I tried to call this onget method in .net core using ajax call but this method hitting without parameters

 public void OnGet(string id,string refundReason ,int amount)
        {           
           
        }

I used these 2 ajax call but not passing parameter values

$.ajax({
    url: "https://localhost:7197/Transactions/Refund?id="+'20a63762-a6ab-4edb-852b-fd247e9dc247'+"&refundReason="+refundReason+"&amount="+amount,
    type: "GET",              
    dataType: "json",
    traditional: true,
    contentType: "application/json; charset=utf-8",
    success: function (data) {
      debugger;
      
    },
});

this one alse tried not passing values to onget method. let me know what changes i’ve to do. Thanks

 $.ajax({
        url: "https://localhost:7197/Transactions/,
        type: "GET",       
        data: JSON.stringify({ id: '20a63762-a6ab-4edb-852b-fd247e9dc247',
                               refundReason:'tt',
                               amount:"1"
                            }), 

2

Answers


  1. You should just be able to call it with $.get and passing the values in the query string. GET calls do not accept body data.

    $.get(`https://localhost:7197/Transactions/Refund?id=20a63762-a6ab-4edb-852b-fd247e9dc247&refundReason=${refundReason}&amount=${amount}`, response => {
        ...
    });
    
    Login or Signup to reply.
  2. First, you should indicate how you want to get parameters from the client. Try using the [FromQuery], [FromBody], [FromForm] attributes on your parameters. You also need to specify your request method: [HttpGet], [HttpPost], etc.

    Second, in the code you posted, you send data from the body and also from the query string. You need use just one of them.

    This should work:

    [HttpGet]
    public void OnGet([FromQuery]string id,[FromQuery]string refundReason, [FromQuery]int amount)
    

    In the AJAX code, remove the data section, and please be sure you are giving the right URL parameter to the AJAX code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search