Asp.net SVC Web服务使用的是代码隐藏,而不是javascript
我想从myClient项目调用项目中的web服务(但在相同的解决方案中)。 我在myClient项目中添加了服务引用。 当我从它背后的代码调用scf时,它可以工作,但当我尝试使用JSON从JavaScript调用它时,我无法做到这一点。伙计们,请帮忙 “这是我的服务之路 Above url someurl是本地主机的url 此代码来自JSON客户端应用程序的a.aspxAsp.net SVC Web服务使用的是代码隐藏,而不是javascript,asp.net,json,web-services,wcf,restful-architecture,Asp.net,Json,Web Services,Wcf,Restful Architecture,我想从myClient项目调用项目中的web服务(但在相同的解决方案中)。 我在myClient项目中添加了服务引用。 当我从它背后的代码调用scf时,它可以工作,但当我尝试使用JSON从JavaScript调用它时,我无法做到这一点。伙计们,请帮忙 “这是我的服务之路 Above url someurl是本地主机的url 此代码来自JSON客户端应用程序的a.aspx $.ajax( { type: 'GET',
$.ajax(
{
type: 'GET',
url: 'http://someurl.com/MyWebService.svc/DoWork/',
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
alert(jqXHR.responseText);
},
success: function (data) {
alert(data);
}
});
从代码隐藏
string postData = "http://someurl.com/MyWebService.svc/DoWork/";
int timeout = 10;
//string dwml = string.Empty;
//MyServiceReference.MyWebServiceClient ms = new MyServiceReference.MyWebServiceClient();
//dwml = ms.DoWork();
//System.Net.WebClient webClient = new System.Net.WebClient();
//dwml = webClient.DownloadString(serviceURL);
//Response.Write(dwml);
HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(postData);
// Set the Method property of the request to POST.
webRequest.Headers.Clear();
webRequest.AllowAutoRedirect = true;
webRequest.Timeout = 1000 * timeout;
webRequest.PreAuthenticate = true;
webRequest.ContentType = "application / x - www - form - urlencoded";
webRequest.Credentials = CredentialCache.DefaultCredentials;
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
webRequest.Timeout = 150000;
// Create POST data and convert it to a byte array.
WebResponse webResponse = null;
StreamReader objSR;
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
Stream objStream;
string sResponse;
webResponse = (HttpWebResponse)webRequest.GetResponse();
objStream = webResponse.GetResponseStream();
objSR = new StreamReader(objStream, encode, true);
//<<sResponse doesn't contain Unicode char values>>
sResponse = objSR.ReadToEnd();
Response.Write(sResponse); // OR Response.write(HttpUtility.HtmlEncode(sResponse))
string postData=”http://someurl.com/MyWebService.svc/DoWork/";
int超时=10;
//string dwml=string.Empty;
//MyServiceReference.MyWebServiceClient ms=新建MyServiceReference.MyWebServiceClient();
//dwml=DoWork女士();
//System.Net.WebClient-WebClient=新系统.Net.WebClient();
//dwml=webClient.DownloadString(serviceURL);
//响应写入(dwml);
HttpWebRequest webRequest=(HttpWebRequest)System.Net.webRequest.Create(postData);
//将请求的Method属性设置为POST。
webRequest.Headers.Clear();
webRequest.AllowAutoRedirect=true;
webRequest.Timeout=1000*超时;
webRequest.PreAuthenticate=true;
webRequest.ContentType=“application/x-www-form-urlencoded”;
webRequest.Credentials=CredentialCache.DefaultCredentials;
webRequest.UserAgent=“Mozilla/4.0(兼容;MSIE 5.01;Windows NT 5.0)”;
webRequest.Timeout=150000;
//创建POST数据并将其转换为字节数组。
WebResponse WebResponse=null;
StreamReader objSR;
System.Text.Encoding encode=System.Text.Encoding.GetEncoding(“utf-8”);
流对象流;
字符串响应;
webResponse=(HttpWebResponse)webRequest.GetResponse();
objStream=webResponse.GetResponseStream();
objSR=新的StreamReader(objStream,encode,true);
// 伙计们,这是第二个问题(都是我问的),只有我自己回答或评论过。我从堆栈溢出的旧问题中得到了ans 4
问题是不允许通过AJAX进行跨域web服务调用。
我遇到了JSONP的新概念,哇,感觉棒极了
但我期待其他成员的快速回复
我不会每次都能救自己的朋友 到目前为止,我已经试过了
SVC代码文件service1.SVC.cs
:
JavaScript函数:
要使用WebGet
,您需要将库System.ServiceModel.Web
添加到您的服务项目中
如果你的基本设置有问题
Web.Config:
注意:这对跨域不起作用,如果您需要,它会被应答。在不同的解决方案中从RESTclient
调用WCF服务,而不使用JSONP
:
在这里,我提出了另一个工作解决方案,在不同的解决方案中从RESTclient
调用WCF服务,而不使用JSONP
,即启用服务(跨源资源共享)策略的CORS。
我们一定都试过:
在服务项目的web配置
文件中添加头Access Control Allow Origin
web配置中的代码:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
<add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
<add name="Access-Control-Max-Age" value="1728000" />
</customHeaders>
</httpProtocol>
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:51058/Service1.svc/GetData",
dataType: 'json',
success: function (data) {
//do success part here
alert(data);
},
error: function (e) {
alert(e.message);
}
});
});
您还可以继续从RESTclient
解决方案到WCF服务的常规AJAX调用:
示例AJAX:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
<add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
<add name="Access-Control-Max-Age" value="1728000" />
</customHeaders>
</httpProtocol>
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:51058/Service1.svc/GetData",
dataType: 'json',
success: function (data) {
//do success part here
alert(data);
},
error: function (e) {
alert(e.message);
}
});
});
最好的部分是,无需在RESTclient
项目解决方案中进行任何修改。有人可以帮助我吗?请将您的问题改为如何使用Javascript中的WCF服务,以便其他人可以快速搜索。看看这一点,您是否尝试过从AJAX调用webservice,获取JSON响应?请重新检查您的问题,没有提到您需要跨域服务调用的任何地方!我的回答中提到了这一点,因为当我提出这个问题时,我并不知道这个问题是跨领域的。
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
<add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
<add name="Access-Control-Max-Age" value="1728000" />
</customHeaders>
</httpProtocol>
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:51058/Service1.svc/GetData",
dataType: 'json',
success: function (data) {
//do success part here
alert(data);
},
error: function (e) {
alert(e.message);
}
});
});