I have created a WCF service and hosted in IIS.
WCF service--
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
[OperationContract]
[WebInvoke(Method ="POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
string[] GetUser(string Id);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public string[] GetUser(string Id)
{ return new User().GetUser(Convert.ToInt32(Id)); }
}
Web Config of WCF--
="1.0"
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="mexBehavior" name="DemoService.Service1">
<endpoint address="DemoSevice" binding="webHttpBinding" bindingConfiguration="" contract="DemoService.IService1"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8090"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>
after that I hosted this service in IIS and now I want to use this service using J Query.
J query code--
<script src="js/jquery-1.8.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
var Type;
var Url;
var Data;
var ContentType;
var DataType;
var ProcessData;
function CallService() {
debugger;
$.ajax({
type: Type,
url: Url,
data: Data,
contentType: ContentType,
dataType: DataType,
processdata: ProcessData,
dataFilter: function (data) { return data; },
success: function (msg) {
debugger;
var jsonData = jQuery.parseJSON(msg);
ServiceSucceeded(jsonData);
},
error: ServiceFailed
});
}
function ServiceFailed(result) {
alert('Service call failed: ' + result.status + '' + result.statusText);
Type = null; Url = null; Data = null; ContentType = null; DataType = null; ProcessData = null;
}
function WCFJSON() {
var uesrid = "1";
Type = "POST";
Url = "http://localhost:8090/DemoService/Service1.svc/GetUser";
Data = '{"Id": "' + uesrid + '"}';
ContentType = "application/json; charset=utf-8";
DataType = "json";
ProcessData = true;
CallService();
}
function ServiceSucceeded(result) {
if (DataType == "json") {
resultObject = result.GetUserResult;
for (i = 0; i < resultObject.length; i++) {
alert(resultObject[i]);
}
}
}
function ServiceFailed(xhr) {
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
$(document).ready(
function () {
WCFJSON();
}
);
</script>
But this block of j query is not working in my end. Where I am doing mistake?. Any idea will be appreciated.