[英]How do I deploy ASP.NET boilerplate WebApi class library as a stand-alone web service?
[英]consume a asp.net web service that returns json from an stand alone html page
我已经在asp.net中开发了一个Web服务,并能够从项目中的aspx页面对其进行测试,并且可以轻松显示以JSON格式返回的信息。
现在,我需要从独立的html页面使用Web服务。
有人对此有经验吗? 我对替换这个的部分感到困惑
<asp:ScriptManager ID="ScriptManager" runat="server">
<Services>
<asp:ServiceReference Path="~\MyService.asmx" />
</Services>
</asp:ScriptManager>
如果直接使用html和javascript无法做到这一点,有人可以向我展示一个独立的php页面吗?
看到这个链接:
http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/
使用jQuery
www.jquery.org
本质上,您使Web Service脚本可调用,只是Web Service定义中的一个属性,然后执行以下操作:
$.ajax({
type: "POST",
url: "~/MyService.asmx/MyMethod",
data: "{parameterName:'" aStringArgument + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
var data = msg.d
// Now var is an object with properties just like your object
}
});
您可以使用Javascript访问您的Web服务。
例如-如果您具有如下定义的json网络服务:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public String Foo(String p1, String p2)
{
return "Hello World";
}
您可以这样称呼它:
var httpobj = getXmlHttpRequestObject();
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject()
{
if (window.XMLHttpRequest)
return new XMLHttpRequest();
else if(window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
}
CallService()
{
//Set the JSON formatted input params
var param = "{'p1' : 'value1', 'p2' : 'value2'}";
//Send it to webservice
if(httpobj.readyState == 4 || httpobj.readyState == 0)
{
httpobj.open("POST", 'service.asmx/' + 'Foo', true);
//Mark the request as JSON and UTF-8
httpobj.setRequestHeader('Content-Type','application/json; charset=utf-8');
httpobj.onreadystatechange = OnSuccess;
httpobj.send(param);
}
}
//Called on successfull webservice calls
OnSuccess()
{
if (httpobj.readyState == 4)
{
//Retrieve the JSON return param
var response = eval("(" + httpobj.responseText + ")");
}
}
如果您不想使用ScriptManager(会在页面上添加超过100k的JS),则可以使用此方法使用jQuery连接到Web服务 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.