kez kez - 11 days ago 4
JSON Question

ajax success response change as @html.raw

I have following jquery code in my Razor viewpage

$(document).ready(function () {

var grouplistvalues = @Html.Raw(Json.Encode(Session["grouplist"]));


$("#nsline").click(function () {

alert(grouplistvalues)

$.ajax({
type: "POST",
url: "SetupGroups",
data: { grouplist : grouplistvalues },
dataType: "html",
success: function (response)
{
grouplistvalues = null;
grouplistvalues = response;
alert(response)
},
error: function ()
{

}
});

});


$("#ewline").click(function () {

$.ajax({
type: "POST",
url: "SetupGroups",
data: { grouplist : grouplistvalues },
dataType: "html",
success: function (response)
{
grouplistvalues = null;
grouplistvalues = response;
},
error: function ()
{

}
});

});


in above
grouplistvalues
its taking session as html raw

when I alert it on
#nsline
click function I can see it,

enter image description here

in above function I'm calling to ajax function and above
grouplistvalues
value updating

once I alert it on
#nsline
click function success response I can see a alert like folllowing
enter image description here

since this(
grouplistvalues
value)
1,2,..
changing as
[1,2..]
I cannot call to other ajax function in
#ewline
click function since parameter difference,

this is the above common ajax call

[HttpPost]
public JsonResult SetupGroups(long[] grouplist)
{
Session["grouplist"] = null;

List<long> groupList = new List<long>();

foreach (var groupitem in grouplist)
{
groupList.Add(groupitem);
}

long[] grouparray = groupList.ToArray();
Session["grouplist"] = grouparray;

return Json(grouparray);
}
}


Though I have two click functions its work with only the first click(ewline or nsline only the first time)

How to solve this

Answer

It was the dataType in your ajax request. It should be json:

dataType: "json"
Comments