【问题标题】:Instantiate a value received from issuing a GET request实例化从发出 GET 请求收到的值
【发布时间】:2014-08-20 09:59:43
【问题描述】:

我有这个 JS 代码,它将接收来自 C# 代码的字符串值

function getMsg() {
var xmlhttp = new XMLHttpRequest(); 
xmlhttp.open("GET", "andSert.asmx/GetMessage", true); //async

var temp = the string I receive from the GET above

return temp;

}

这里是 C# 代码

[WebMethod]
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;
}

我的问题是我不知道如何在 JS 代码中实例化通过 GET 请求获得的消息。我已经测试过一切正常,并返回一个字符串。但是我需要实例化那个字符串,以便在另一个 JS 文件中使用它。

我该怎么做?

【问题讨论】:

  • 如果您使用的是异步请求,那么您将无法返回该值,因为在执行 return 语句之前您将没有字符串
  • 好的,我把它改成 false,所以它是同步的
  • 你会用jQuery吗?
  • @OndrejSvejdar,您将如何阻止该功能的其余部分?
  • 是的,我想我可以,如果我找不到使用纯 JS 的解决方案 :) 在这种情况下,我知道如何解决它。但是这个系统完全使用了这种方法,所以开始抛出 JQuery 会使代码甚至偏离现在的样子。我还需要获取图书馆等

标签: c# javascript asp.net web-services webmethod


【解决方案1】:

您可能需要考虑改用 jQuery $.ajax,因为它可以保护您免受浏览器怪癖的影响。

同步解:

function getMsg() {
  var msg = "";
  xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    { 
      msg = JSON.parse(xmlhttp.responseText).d;
    }
  };
  xmlhttp.open("GET","andSert.asmx/GetMessage",false);
  xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  xmlhttp.send();
  return msg;
}

异步解决方案:

function getMsg(fCallback) {
  var msg = "";
  xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      fCallback(JSON.parse(xmlhttp.responseText).d);
    }
  };
  xmlhttp.open("GET","andSert.asmx/GetMessage",true);
  xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  xmlhttp.send();
}
getMsg(function(message) { alert(message); });

还必须适当地装饰服务:

using System.Web.Script.Services;
using System.Web.Services;

namespace Whatever {
  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  [System.ComponentModel.ToolboxItem(false)]
  [ScriptService]
  public class andSert : System.Web.Services.WebService {
    [ScriptMethod(UseHttpGet = true)]
    [WebMethod]
    public string GetMessage() {
      return "Hello World";
    }
  }
}

请注意,类的名称应类似于 class MyFavouriteClass,使用 class andSert 来匹配您的问题。

【讨论】:

  • xmlhttp.responseText 返回 "scandiatransplant.org">This is a test" 我只想要显示值说“这是一个测试”。我想我可以通过使用 string.split 来获得这个值,但这是一个非常丑陋的解决方案。有没有其他方法可以单独获得这个值?
  • @user1960836 - 用 [ScriptMethod(UseHttpGet=true)] 属性装饰你的 C# 方法
  • 我添加了这个 [WebMethod(UseHttpGet = true)] 并得到一个错误,说 WebMethodAttribute 不包含 UseHttp 的定义。查找 WebMethod 及其属性,UseHttp 不是其中之一。我做错了什么?
  • @user1960836 - 我说的是 ScriptMethod,而不是 WebMethod :)
  • 这并没有解决它,我仍然得到带有 xml 标签的字符串。 [WebMethod(UseHttpGet = true)] 在这种情况下做什么?根据我通过阅读文档的理解,它只会强制方法只能通过 get 调用?
猜你喜欢
  • 1970-01-01
  • 2011-09-30
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多