开发者

jquery ajax error when iterating through result set from asp.net mvc method

Calling an asp.net mvc3 controller method which returns the results fine (list of an object) and passes the list (c#) to the following jQuery Ajax function:

    //$("#yourtableid > tr").remove();
function getAnswers(qid) {
      var url = "/rankings/getanswer开发者_开发技巧s/" + qid;
      $.ajax({
      type: "POST",
      url: url,
      data: {qid: qid}, //"{}",
      dataType: "json",
      success: function(response) {
        var cars = response;
        $('#rankings').empty();
        var i = 0;
        debugger;
        $.each(cars, function(index, car) {
          $('#rankings').append(
           "<tr rorder=\"" + car.Ranking + "\"><td>" 
           + ++i.toString() + "</td><td align=\"left\"><span onclick=\"adjustRank('" 
           + car.Id + "')\" style=\"cursor:pointer;\">" 
           + car.Text + "</span></td><td></td></tr>"
          );
        });
      },
      failure: function(msg) {
        $('#rankings').text(msg);
      }
    });
}

I don't see anything wrong with this but I get the following error:

Microsoft JScript runtime error: Cannot assign to a function result


I think ++i.toString() is invalid. What did you want to achieve? (++i).toString() ?


did you convert the array to a json in your action code.. you could do it as follows:

new JavaScriptSerializer().Serialize(array);

also a suggestion to make this code readable and maintainable

var stringToAppend= $("<tr order=\"" + car.Ranking + "\"><td>")
.append($((++i).toString() + "</td><td align=\"left\"><span onclick=\"adjustRank('") 
.append( car.Id + "')\" style=\"cursor:pointer;\">") 
.append(car.Text + "</span></td><td></td></tr>")

and replace the double quotes with single quotes for the strnigs and dnt escape the double quotes

What this does it guarantees that your markup is well built else jquery will throw an error and the code is readable

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜