如何使用JavaScript获取Web服务XML

时间:2014-08-29 16:01:40

标签: java xml http get

如何将Java Script与HTTP Web服务一起使用,这是HTTP get请求:

 'GET /stockquote.asmx/GetQuote?symbol=string HTTP/1.1
  Host: www.webservicex.net
  HTTP/1.1 200 OK
  Content-Type: text/xml; charset=utf-8
  Content-Length: length

  <?xml version="1.0" encoding="utf-8"?>
  <string xmlns="http://www.webserviceX.NET/">string</string>'

我在堆栈上发现了这个Java,但我无法让它工作:

 '$.get(
  "somepage.php",
  {paramOne : 1, paramX : 'abc'},
  function(data) {
   alert('page content: ' + data);
  }
  );'

2 个答案:

答案 0 :(得分:0)

您找到的代码无法正常工作的原因在于它包含在单引号中,并且取决于名为jQuery的第三方库。如果从页面链接到jQuery(并删除jQuery字符串周围的单引号)并使用正确的参数指向$ .get方法中的正确URL,它可能会正常工作。

答案 1 :(得分:0)

我在C#中完成了这个

 'Using System.Net;

使用System.IO;

public class GetData
{
    public static string HTTP_GET(string Url, string Data)
    {
        string Out = String.Empty;
        System.Net.WebRequest req = System.Net.WebRequest.Create(Url + (string.IsNullOrEmpty(Data) ? "" : "?" + Data));
        try
        {
            System.Net.WebResponse resp = req.GetResponse();
            using (System.IO.Stream stream = resp.GetResponseStream())
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(stream))
                {
                    Out = sr.ReadToEnd();
                    sr.Close();
                }
            }
        }
        catch (ArgumentException ex)
        {
            Out = string.Format("HTTP_ERROR :: The second HttpWebRequest object has raised an Argument Exception as 'Connection' Property is set to 'Close' :: {0}",
        }
        catch (WebException ex)
        {
            Out = string.Format("HTTP_ERROR :: WebException raised! :: {0}", ex.Message);
        }
        catch (Exception ex)
        {
            Out = string.Format("HTTP_ERROR :: Exception raised! :: {0}", ex.Message);
        }

        return Out;
    }
}

[System.Web.Services.WebMethod]
    public static string getQuote()
      {  
        XmlDocument quoteXML = new XmlDocument();
        string strQuote =       GetData.HTTP_GET("http://www.mywebservice/stockquote.asmx/GetQuote", "symbol=lloy.l");
    return strQuote;
      }'