1

Is status a useable JSON object?

How can I access the values of action_success and newIndex on status?

Server:

[HttpPost]
public ActionResult UploadFiles()
{
    // save file..
    return Json(new { action_success = "true", newIndex = 2 });
}

Client:

complete: function (e, data) {
    var status = e.currentTarget.response;
    // From FireBug: status is "{"action_success":"true","newIndex":2}"
    // Including the first and last double-quote.

    // I want to do something like:
    // status.action_success and status.newIndex but I can't!    
}

EDIT: Simple solution:

var statusParsed = JSON.parse(status);
var success = statusParsed.action_success;
var index = statusParsed.newIndex;
radbyx
  • 9,352
  • 21
  • 84
  • 127
  • 1
    hmm I need JSON.parse()? – radbyx May 27 '14 at 08:54
  • I didn't knew about JSON.parse() when I wrote the question. I was something I found after. I'm trying it now, don't hate me. – radbyx May 27 '14 at 08:57
  • Which AJAX API is used in JavaScript, and how is it initiated? (jQuery will normally default to auto-parsing JSON.) – user2864740 May 27 '14 at 08:57
  • I can access action_success and newIndex by "." from status now, after adding this line: ` var statusParsed = JSON.parse(status); ` – radbyx May 27 '14 at 08:59
  • @user2864740 I don't know what you mean. I can tell you my jQuery version and the version of wijmo if it's that? It's a wijUpload function you see on my client side. – radbyx May 27 '14 at 09:01
  • What do you see if you `console.log(data);` inside the complete function ? – Paul May 27 '14 at 09:05
  • data contains alot of things. It's not possible to say them all. – radbyx May 27 '14 at 09:10

1 Answers1

0

i think you can use direct JsonResult and should work

[HttpPost]
public JsonResult UploadFiles()
{
// save file..
return Json(new { action_success = "true", newIndex = 2 });
}

and on client side if you are using $.ajax() then set datatype as json

rjdmello
  • 865
  • 2
  • 9
  • 14