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))
here I have tried so far
SVC code file
service1.svc.cs
:JavaScript function:
All above code you might have tried, some important note to get this worked is:
1. as WCF .svc works on Representational State Transfer (REST), you have to explicitly mention data get request in
service1.svc
Markup file,To use
WebGet
,you will need to add librarySystem.ServiceModel.Web
to your service Project.And if you have issues with basic settings then,
Web.Config:
NOTE: This will not work for cross domain, if you want that, its answered Here.
calling WCF service from
RESTclient
in different solution withoutJSONP
:Here I came up with another working solution for, calling WCF service from
RESTclient
in different solution without usingJSONP
i.e. Enabling CORS of service (Cross Origin Resource Sharing) policy. We all must have tried:Adding Header
Access-Control-Allow-Origin
inweb-config
file of Service Project,Code in web-config :
but anyhow, that didn't worked out for me!
So, there is another way to achieve the same, is to Create a
Global.asax
in Service Project and Add this code to theGlobal.asax.cs
:Code in Global.asax.cs :
And you can continue with your regular AJAX call from
RESTclient
solution to WCF service:Sample AJAX :
The best part is, no need to do any modifications in
RESTclient
project solution.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!