Javascript jQuery/AJAX,检索我的方法返回的字符串
我有以下代码:Javascript jQuery/AJAX,检索我的方法返回的字符串,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有以下代码: [WebMethod] [ScriptMethod(UseHttpGet=true)] public string GetMessage() { XmlTextReader reader = new XmlTextReader (Global.sAppPath + "/alt/importantMsg.xml"); string message = null; while (reader.Read())
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public string GetMessage() {
XmlTextReader reader = new XmlTextReader (Global.sAppPath + "/alt/importantMsg.xml");
string message = null;
while (reader.Read()) {
if (reader.IsStartElement ()) {
switch (reader.Name.ToString ()) {
case "Message":
message = reader.ReadString();
break;
}
}
}
return message;
}
我想使用jQuery检索消息(代码隐藏返回的字符串)。所以我有这个代码:
$(document).ready(function () {
$.get("isoServe.asmx/GetMessage", function(data, status) {
alert("Data: " + data + "\nStatus: " + status);
});
}))
但这对我不起作用。好像我打不穿洞似的。我做错了什么?
我也试过:
$(document).ready(function () {
$.ajax({
url: "isoServe.asmx/GetMessage",
data: "message",
dataType: "json",
success: function (data) {
alert(data);
}
});
});
但这最后一段代码,我非常不确定。“数据”应如何在本文件中给出
下面的示例确实有效(使用xmlhttp)。但是我想使用jQuery
function getMsg() {
var msg = "";
xmlhttp = gus.tie = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
msg = xmlhttp.responseXML.documentElement.textContent;
alert("Msg: " + msg + ": fra getMsg()");
}
};
xmlhttp.open("GET", "isoServe.asmx/GetMessage", false);
xmlhttp.send();
return msg;
}
抱歉,由于某些原因,url中引用的文件已被删除。这就是它找不到它的原因。我似乎有很多调试要做,天哪。祝我好运。
感谢您的时间成功回调包含
数据
参数:成功:函数(数据){alert(数据);}
,查找文档谢谢,这是错误遗漏的,c&p的客户。这没有任何区别,它仍然不会提醒你,你确定url是正确的吗?如果您直接在浏览器中访问url会发生什么是的,url是正确的。如果我使用xmlhttp,那么我可以访问该url。但是我不想使用xmlhttp,因为我在状态0方面遇到了问题,并且在搜索此错误时,建议使用JQueryis返回的字符串是有效的json字符串吗?