I’m really new at this jquery stuff. I have a wcf web service running :
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
[WebInvoke(Method="GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle=WebMessageBodyStyle.Wrapped)]
string SayHello();
}
public class HelloWorlService : IHelloWorldService
{
public string SayHello()
{
return "Hello ";
}
}
When I enter http://localhost:62604/HelloWorld.svc/SayHello into google chrome i get the following result :
{“SayHelloResult”:”Hello “}
So it appears to be working.
Now I create a default.html that looks like this:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function CallService() {
$.ajax({
url: "http://localhost:62604/HelloWorld.svc/SayHello",
type: "GET",
dataType: "json",
processdata = false,
contentType: "application/json; charset=utf-8",
sucess:function(data) { alert('success'); },
error: function (e) { alert('failed'); }
});
}
$(document).ready(function () {
$("a").click(function () {
CallService();
});
});
</script>
</head>
<body>
<a href="">Link</a>
</body>
</html>
So really simple stuff … but it’s not working … i always get alert failed message ….
I’m a bit lost here, not sure what is wrong.
Any help is appreciated.
EDIT: the default.html page is not hosted on a webserver, its just a simple file that I open on a browser. Could that be the reason why it’s not working correctly?
The problem with your jQuery is, as far as I can tell, really simple to fix. Your “URL” parameter, is an absolute URL. Browser security settings restrict the use of URLs like this. Try using a relative URL to grab the data.
If you have to use an absolute URL, then have your server fetch the data from the external URL, then give it to jQuery in such a ways that only a relative URL is required.
Hope that helps.