我有一个ajax脚本,该脚本以与以往相同的方式调用Web方法,但是现在无法获得响应。我解析错误。请帮忙。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void UzmiTaskove(int idprojekta)
{
List<Taskovi> listataskova = new List<Taskovi>();
string CS = Properties.Settings.Default.Konekcija.ToString();
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd4 = new SqlCommand("UzmiTaskove", con);
cmd4.CommandType = CommandType.StoredProcedure;
con.Open();
cmd4.Parameters.AddWithValue("@IdProj", idprojekta);
SqlDataReader rdr = cmd4.ExecuteReader();
while (rdr.Read())
{
Taskovi tas = new Taskovi();
tas.ID = Convert.ToInt32(rdr["ID"]);
tas.IdProjekta = Convert.ToInt32(rdr["IdProjekta"]);
tas.Opis = rdr["Opis"].ToString();
tas.DatumPocetka = rdr["DatumPocetka"].ToString();
tas.DatumZavrsetka = rdr["DatumZavrsetka"].ToString();
tas.Status = rdr["Status"].ToString();
listataskova.Add(tas);
}
}
JavaScriptSerializer jm = new JavaScriptSerializer();
Context.Response.Write(jm.Serialize(listataskova));
}
$.ajax({
url: 'TaskoviServis.asmx/UzmiTaskove',
type: 'POST',
data:JSON.stringify({ idprojekta:idprojekta}),
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (respons) {
alert(respons.d);
var newHtml = "";
$(poda).each(function (index, tas) {
newHtml += '<li class="task">';
newHtml += '<div class="task-container">';
newHtml += '<span class="task-action-btn task-check">';
newHtml += '<span class="action-circle large complete-btn" title="Mark Complete">';
newHtml += '<i class="material-icons">check</i>';
newHtml += '</span>';
newHtml += '</span>';
newHtml += '<span class="task-label" contenteditable="true">' + tas.Opis + '</span>';
newHtml += '<span class="task-action-btn task-btn-right">';
newHtml += '<span class="action-circle large" title="Assign">';
newHtml += '<i class="material-icons">person_add</i>';
newHtml += '</span>';
newHtml += '<span class="action-circle large delete-btn" title="Delete Task">';
newHtml += '<i class="material-icons">delete</i>';
newHtml += '</span>';
newHtml += '</span>';
newHtml += '</div>';
newHtml += '</li>';
});
$("#task-list").append(newHtml);
},
error: function (xhr, textStatus, error) {
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}
});
我尽了一切..更改数据类型..等等...
答案 0 :(得分:0)
好了,这个方法给您带来了麻烦,因为它使问题无处不在,所以在ajax代码中什么都不要更改,只要更改webmethod代码的make返回类型和返回值而不是response.write,直接更改ajax代码就可以了。 / p>
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Taskovi> UzmiTaskove(int idprojekta)
{
List<Taskovi> listataskova = new List<Taskovi>();
string CS = Properties.Settings.Default.Konekcija.ToString();
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd4 = new SqlCommand("UzmiTaskove", con);
cmd4.CommandType = CommandType.StoredProcedure;
con.Open();
cmd4.Parameters.AddWithValue("@IdProj", idprojekta);
SqlDataReader rdr = cmd4.ExecuteReader();
while (rdr.Read())
{
Taskovi tas = new Taskovi();
tas.ID = Convert.ToInt32(rdr["ID"]);
tas.IdProjekta = Convert.ToInt32(rdr["IdProjekta"]);
tas.Opis = rdr["Opis"].ToString();
tas.DatumPocetka = rdr["DatumPocetka"].ToString();
tas.DatumZavrsetka = rdr["DatumZavrsetka"].ToString();
tas.Status = rdr["Status"].ToString();
listataskova.Add(tas);
}
}
// JavaScriptSerializer jm = new JavaScriptSerializer();
// Context.Response.Write(jm.Serialize(listataskova));
return listataskova;
}
并在ajax中进行如下更改
$.ajax({
url: 'TaskoviServis.asmx/UzmiTaskove',
type: 'POST',
data:JSON.stringify({ idprojekta:idprojekta}),
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (respons) {
//alert(respons.d);
console.log(respons.d)
var newHtml = "";
$(respons.d).each(function (index, tas) {
newHtml += '<li class="task">';
newHtml += '<div class="task-container">';
newHtml += '<span class="task-action-btn task-check">';
newHtml += '<span class="action-circle large complete-btn" title="Mark Complete">';
newHtml += '<i class="material-icons">check</i>';
newHtml += '</span>';
newHtml += '</span>';
newHtml += '<span class="task-label" contenteditable="true">' + tas.Opis + '</span>';
newHtml += '<span class="task-action-btn task-btn-right">';
newHtml += '<span class="action-circle large" title="Assign">';
newHtml += '<i class="material-icons">person_add</i>';
newHtml += '</span>';
newHtml += '<span class="action-circle large delete-btn" title="Delete Task">';
newHtml += '<i class="material-icons">delete</i>';
newHtml += '</span>';
newHtml += '</span>';
newHtml += '</div>';
newHtml += '</li>';
});
$("#task-list").append(newHtml);
},
error: function (xhr, textStatus, error) {
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}
});
尝试这应该可行。