3

I am using an AJAX call to display information based on success or failure of some logic written in C#. I now need to return additional data back from the server and display it. The data I need is contained in the employers and agencies variables. How can I return that data in my return Json statement along with the success?

$.ajax({
    url: rootpath + "/clients/hasDuplicateTaxId",
    type: "GET",
    success: function (data) {
        if (data.success) {
            // success
        }
        else {
            // fail
        }
    },
    error: function (response) {
        var error = response;
    }
});
if (taxIdExists)
{
    var employers = _employerRepository.GetByTaxId(taxId).ToList();
    var agencies = _employerRepository.GetByTaxId(taxId).Select(e => e.GeneralAgency).ToArray();
    return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}

return Json(new { success = false, error = "Error" }, JsonRequestBehavior.AllowGet);
4
  • 2
    Add the employers and agencies variables to the anonymous type you provide to the Json response. You can then access the properties/objects/arrays they expose in the data variable of your JavaScript. It's impossible to give you anything more concrete than that, as you haven't shown us what those classes actually look like. Commented Jan 22, 2016 at 16:49
  • 2
    See what you did with the error member in the C# code? Do the same thing for the other ones too. Commented Jan 22, 2016 at 16:49
  • how would that look? something like this? return Json(new { success = true, employers = employers, agencies = agencies }, JsonRequestBehavior.AllowGet); Commented Jan 22, 2016 at 16:58
  • That's right. I added an answer with an example for you. Commented Jan 22, 2016 at 17:01

1 Answer 1

1

You need to add the employers and agencies variables to the anonymous type you provide to the Json, like this:

if (taxIdExists)
{
    var employers = _employerRepository.GetByTaxId(taxId).ToList();
    var agencies = _employerRepository.GetByTaxId(taxId).Select(e => e.GeneralAgency).ToArray();
    return Json(new { success = true, employers, agencies }, JsonRequestBehavior.AllowGet);
}

From there you can access the arrays stored in those properties in the success handler of your $.ajax call:

success: function (data) {
    if (data.success) {
        console.log(data.employers);
        console.log(data.agencies);
    }
    else {
        // fail
    }
},

They will be arrays, so you would need to loop through them to extract the required information.

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

5 Comments

After I add those variables to my anonymous type, I drop into the error setting of my Ajax request.
What's the error you're getting? You can set a breakpoint to find it, or check the state of the request in the console.
I set a breakpoint and it hits inside of the `error: function (response)' How do I tell what the error is?
Use the network tab of the console, or set the breakpoint in your C# code to step through.
Ah, a circular reference was detected while serializing the Employer object

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.