[英]Calling a javascript function from a global file and return it to append to html
I have a global javascript file 'Global.js' with a Global Handler 'GlobalHandler.ashx', what i am trying to do is to render some data in the back-end (inside the handler ), than return text data using
context.Response.Write(MyString)
我有一个带有全局处理程序'GlobalHandler.ashx'的全局javascript文件'Global.js',我想要做的是在后端(在处理程序内部)呈现一些数据,而不是使用
context.Response.Write(MyString)
返回文本数据context.Response.Write(MyString)
The question is how to append the data to my html element . 问题是如何将数据附加到html元素。 I looked into the response (200) and the data is there , but i don't know the reason of not appending my text into the html element
我调查了响应(200),并且数据在那里,但是我不知道不将我的文本附加到html元素中的原因
I have tried to append them like the classic way success:function(data){ $(elementID).html(data);}
But that doesnt work 我试图像
success:function(data){ $(elementID).html(data);}
的经典方式一样附加它们success:function(data){ $(elementID).html(data);}
但这success:function(data){ $(elementID).html(data);}
Here In Global.js 在Global.js中
function GetProfession(elementID) {
$.ajax({
url: "/Handlers/GlobalHandler.ashx",
dataType: "JSON",
contentType: "application/json;charset=utf-8",
//responseType: ResponseType,
data: {
functionName: "GetProfession"
},
success: function (data) {
return $("#" + elementID).html(data);
}
});
}
Here In MyPage.aspx 在MyPage.aspx中
$(document).ready(function () {
GetProfession("Profession");
});
HERE In the Handler 在处理程序中
string functionName = context.Request["functionName"];
GroupDAO GroupDAO = new GroupDAO();
if (functionName.Equals("GetProfession"))
{
var ListOfGroups = GroupDAO.GetGroups();
string Builder = "";
foreach (var group in ListOfGroups)
{
Builder+="<option value='" + group.GroupID + "'>" + group.GroupName + "</option>";
}
context.Response.Write(Builder);
}
I am expecting to have those options appended to the html element 'Profession' but this unfortunately it does not happening 我希望将这些选项附加到html元素“ Profession”上,但是不幸的是,它没有发生
I found the answer , i did not recognize the logical reason of such behaviour , but the data was not in the success method even if the statuc code was 200 . 我找到了答案,虽然我不认识这种行为的逻辑原因,但是即使状态码为200,数据也不在成功方法中。 in fact it was in the error: properties of ajax request .
实际上这是在错误中:ajax request的属性。 what i have done is : instead of appending the data in success to the html element .
我所做的是:而不是将数据成功附加到html元素。 i did it in the response text .
我在回复文本中做到了。
Here is the code before not working : 这是无法使用之前的代码:
function GetProfession(elementID) {
$.ajax({
url: "/Handlers/GlobalHandler.ashx",
dataType: "JSON",
contentType: "application/json;charset=utf-8",
//responseType: ResponseType,
data: {
functionName: "GetProfession"
},
success: function (data) {
return $("#" + elementID).html(data);
}
});
}
Here is the one that works 这是可行的
function GetProfession(elementID) {
$.ajax({
url: "/Handlers/GlobalHandler.ashx",
dataType: "JSON",
contentType: "text/html; charset=utf-8",
//responseType: ResponseType,
data: {
functionName: "GetProfession"
},
success: function (data, fata, meta) {
},
error: function (err) {
$("#Profession").html(err.responseText);
//alert(err.responseText);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.