SVC Web服务从code从JavaScript消耗落后,但不 [英] SVC Web service consuming from code behind but not from javascript
问题描述
我要打电话从myClient项目的Web服务项目(但在同一个解决方案)。
我加入的myClient项目服务引用。
当我打电话从背后code SCF,工作,但是当我尝试使用JSON从JavaScript调用它,我不能这样做。伙计们请帮助。
http://someurl.com/MyWebService.svc/DoWork/ 是我的服务路径
abovive网址someurl为localhost的网址
这code是JSON的客户端应用程序,
的a.aspx $阿贾克斯(
{
输入:GET,
网址:'http://someurl.com/MyWebService.svc/DoWork/',
的contentType:应用/ JSON的;字符集= UTF-8,
数据:{},
数据类型:JSON
错误:功能(jqXHR,textStatus,errorThrown){
警报(errorThrown);
警报(jqXHR.responseText);
},
成功:功能(数据){
警报(数据);
} });
从code后面
字符串POSTDATA =http://someurl.com/MyWebService.svc/DoWork/;
INT超时= 10;
//字符串dwml =的String.Empty;
//MyServiceReference.MyWebServiceClient毫秒=新MyServiceReference.MyWebServiceClient();
// dwml = ms.DoWork();
//System.Net.WebClient Web客户端=新System.Net.WebClient();
// dwml = webClient.DownloadString(serviceURL中);
//Response.Write(dwml); HttpWebRequest的WebRequest的=(HttpWebRequest的)System.Net.WebRequest.Create(POSTDATA);
//设置请求POST的Method属性。
webRequest.Headers.Clear();
webRequest.AllowAutoRedirect = TRUE;
webRequest.Timeout = 1000 *超时;
WebRequest的preAuthenticate = TRUE。
webRequest.ContentType =应用程序/ x - WWW - 形式 - urlen codeD;
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 EN code = System.Text.Encoding.GetEncoding(UTF-8);
流objStream;
串sResponse; WebResponse类=(HttpWebResponse)webRequest.GetResponse();
objStream = webResponse.GetResponseStream();
objSR =新的StreamReader(objStream,连接code,真);
//<< sResponse不包含统一code char值>>
sResponse = objSR.ReadToEnd();
的Response.Write(sResponse); //或者RESPONSE.WRITE(HttpUtility.HtmlEn code(sResponse))
这家伙立刻第二个问题(均由我只问了),这只有我自己已经回答或评论。我答4这堆栈溢出老问题结果
基本例)JSONP?
问题是与跨域的Web服务调用不通过AJAX不允许的。
我碰到JSONP的新概念,哇感觉很棒!
不过,我期待从堆栈快速回复溢出的其他成员。
我将无法拯救自己每一次的朋友!
I want to call web service in project (but in same solution) from myClient project. I have added service reference in myClient project. When I call scf from code behind it, works but when I try to call it from JavaScript using JSON, I am unable to do so. Guys pls help.
"http://someurl.com/MyWebService.svc/DoWork/" is path of my Service
abovive url someurl is url of localhost
This code is from a.aspx of client application of JSON,
$.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);
}
});
From Code behind
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))
Guys this immediate second question (both asked by me only) which only myself has answered or commented. I got ans 4 this from stack overflows old question
Basic example of using .ajax() with JSONP?
Issue was with cross domain web-service call is not allowed through AJAX. I came across new concept of JSONP, wow feeling great!
But I was expecting quick reply from Stack overflows other members.
I will not be able to rescue myself every time friends!
这篇关于SVC Web服务从code从JavaScript消耗落后,但不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!