Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
Parse return value from $ajax call in JavaScript from MVC Controller

I'm using VS2022 with C# and I'm trying to use an $ajax get method in my JavaScript function. I'm sending a parameter and returning a string[] list but when I receive the return I use JSON.stringify but when I try to use JSON.Parse it fails. The Javascript code is

$.ajax({
    type: "GET",
    url: '/Home/GetCategories/',
    contentType: 'application/json', 
    datatype: 'json',
    data: { ctgData: JSON.stringify(relvalue)}
}).done(function(result) {
    let userObj = JSON.stringify(result); 
    let resultList = JSON.parse(userObj);
});

The code in the controller which returns the list is simple at the moment until I get the return working

[HttpGet]
public ActionResult GetCategories(string ctgData)
{
    string[] categoryList = { "Food & Drink", "Sport & Fitness", "Education", "Housework", "Fiction", "Horror Books", "Fantasy", "Magic" };
    return Json(new { data = categoryList });
}

The value in result is

{"data":["Food & Drink","Sport & Fitness","Education","Housework","Fiction","Horror Books","Fantasy","Magic"]}

I've tried a number of different ways in Parse but it always fails, can you tell me what I'm missing to get my resultList to contain the string array.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You don' t need to parse anything. It is already java script object

$.ajax({
    type: "GET",
    url: '/Home/GetCategories/',
    contentType: 'application/json', 
    datatype: 'json',
    data: { ctgData: JSON.stringify(relvalue)}
     sucess:  function(result) {
      let data=result.data; // data = ["Food & Drink","Sport & Fitness",..]
   }
});
about 4 years ago · Juan Pablo Isaza Report

0

use this:

JSON.parse(JSON.stringify(data))

const data = {"data":["Food & Drink","Sport & Fitness","Education","Housework","Fiction","Horror Books","Fantasy","Magic"]}

const resultJSON = JSON.parse(JSON.stringify(data))

console.log("json >>>", resultJSON)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!