I’ve searched for this and I’ve not been able to find something that helps me so I appologise if this has been posted and I’ve just been unable to find it.
I’ve created a WCF service application that is being hosted in IIS. Presently its very basic with just a hello world method basically to return a country name and its code as a json object.
I’ve also written some jquery that will call the method remotely with the aim of populating list objects.
Currently when I call the method it hits the success paramater of the ajax call and alerts me with “undefined” I’ve no idea whats causing this but its most likely I’ve made a silly mistake.
Heres the code of the service and jquery
web config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="None" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint crossDomainScriptAccessEnabled="true"/>
</webScriptEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
service1.svc
<%@ ServiceHost Language="C#" Debug="true" Service="RestfulFlightWCF.Service1" codeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>
service1.svc.cs
{
// NOTE: You can use the “Rename” command on the “Refactor” menu to change the class name “Service1” in code, svc and config file together.
[ServiceContract(Namespace = "JsonpAjaxService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public Country GetCountry(string id)
{
Country county = new Country();
county.Name = "United Kingdom";
county.Id = "gb";
return county;
}
[DataContract]
public class Country
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
}
}
jquery
$(document).ready(
function () {
$.ajax({
type:"GET",
Data:'gb',
Url:"http://192.168.1.6:80/FlightServices.svc/GetCountry",
DataType:"jsonp",
method:"GetCountry",
success: function(msg){
debugger;
alert(msg.responseText);
if (msg.responseText) {
var err = msg.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
},
failure: function(){
alert("something went wrong");
},
error: function(){
alert("something happned");
}
});
});
Sorry for the long post but I thought it would help if I included my code.
Ok So I got this working tonight after fiddling about for a while so I thought I’d post up some of the stuff that I found out whilst looking for the fix.
Add in response format to the WebGet on the method being called.
Changed my jQuery to the following:
key point I found was passing the parameters down to the webGet method as