Process JSON data to form input fields

I've got a jQuery Ajax response with JSON data that I need to process and having problems with. Each JSON "key" represents a form input field name attribute which it's associated data needs to go in to.

Currently my jQuery Ajax success function has the following which doesn't work...

success: function(data) {

  if(data.Status == "Success"){

     $.each(data, function(key,value){
        $("input[name='" + key + "']").val(value);
     );

  }else{
    // Error stuff
  }
}

Any help greatly appreciated.... I'm pulling my hair out here over something which I'm sure should be quite simple.

Dan

Answers


The correct code: (you missed a })

success: function(data) {

  if(data.Status == "Success"){

     $.each(data, function(key,value){
        $("input[name='" + key + "']").val(value);
     }); // here you made mistake

  }else{
    // Error stuff
  }
}

If the above code not working, please use console.log in Google Chrome and see the values for key and value pair from each statement.


If response is actually a JSON string instead of a real JS Object, you should use jQuery.parseJSON(data) to actually turn it into a JS object first before using it, iterating over it, etc.


Need Your Help

Display returned value from Class method to Form

c# .net

In Form GroupExmStart I am calling Foo method from Question class and storing all Questions in var questions and passing it to int Quiz method,how can I store this question in string[] so that i can

Get rid of gray line in R density plot density at y = 0

r plot postscript eps density-plot

I have a density plot that I want to save in an eps file.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.