2

I have a ajax call that goes like

this.GetTransactionsInputToday = function () {
             var status="complete"
             $.ajax({
                 url: '/Management/function1',
                 contentType: "application/json; charset=utf-8",
                 data: status,
                 type: 'GET',
                 cache: false,
                 success: function (result) {
                     return result;
                 }
             });
         }

I have also tried doing like this

this.GetTransactionsInputToday = function () {
             var status="complete"; 
              $.ajax({
                     url: '/Management/function1/' + status,
                     type: 'GET',
                     cache: false,
                     success: function (result) {
                         return result;
                     }
                 });
             }

I have a controller function in my management controller class

public JsonResult function1(string status)
{
Some code here.. 
}

The issue is that every time function1 is called teh value of status comes as null. CAn any one please let me know where am I going wrong??

1
  • please change the title because the question is above JOSNstring not simple string Commented Apr 21, 2021 at 18:06

3 Answers 3

17

You need to define name for the data you sending data: {'status': status}:

this.GetTransactionsInputToday = function () {
    var status="complete"
    var r = '';

    $.ajax({
        url: '/Management/function1',
        contentType: "application/json; charset=utf-8",
        data: {'status': status},
        type: 'GET',
        cache: false,
        success: function (result) {
            r = result;
        }
    });

    return r;
};

Also your this.GetTransactionsInputToday will not return result as you expected. The success handler of ajax function gets called asynchronously. So your return r statement will return '' as it gets called before the Ajax request gets completed.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to achive this: func = 'getDetails'; data = 'loads of data'; and then in $.ajax({ ... data: { func : data }. ? now it sends string named func so in php is $_POST['func'], i want to send string content as name to php not 'func' but $_POST['getDetails']. Any idea ?
2

You might construct the json parameter wrongly.

using json: data: "{'prop1': '" + value1+ "'}"

$.ajax({
             url: '/Management/function1',
             contentType: "application/json; charset=utf-8",
             data: "{'prop1': '" + value1+ "'}",
             type: 'GET',
             cache: false,
             success: function (result) {
                 return result;
             }
         });

Comments

0

Try setting the variable outside the ajax function and then return it, this way its in the right scope. Also in order to get an immediate value you need to make this ajax call synchronous.

this.GetTransactionsInputToday = function () {
    var status = "complete";
    var r = '';

    $.ajax({
        async : false,
        url: '/Management/function1',
        contentType: "application/json; charset=utf-8",
        data: {'status' : status},
        type: 'GET',
        cache: false,
        success: function (result) {
            r = result;
        }
    });

    return r;
};

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.