0

I have the following script in my web page to call out to a JSON-enabled WCF service that I have created and am hosting on my web server. However, the alert displays "null". When I point to the url in a browser it returns very simple JSON: {"city":"Ann Arbor"}. Also, when I run the page containing the code below with Fiddler running, I can see the service is hit and the JSON returns. But still the success function below returns null. Anyone know what I'm doing wrong? Thanks. -Ned

<script type="text/javascript">
    $.ajax({
        type: "GET",
        url: "http://192.168.192.17:8080/Service.svc/class/",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            alert(data);
        }
    });
</script>

2 Answers 2

1

I suspect the problem comes from the fact that you are trying to call a web service using AJAX which is not hosted on the same domain as the calling script and thus you are violating the same origin policy. If you want to be able to call http://192.168.192.17:8080/Service.svc using AJAX, the calling script needs to be hosted also on http://192.168.192.17:8080.

As a possible workaround you could use a server side script acting as bridge hosted on the same domain as the client script or use JSONP if you have control over the web service.

Sign up to request clarification or add additional context in comments.

1 Comment

I ended up hosting it on the same domain which is okay for now. Thanks for your help.
0

You could define an error function to see what the problem is:

$.ajax({
  error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert(errorThrown)
  },
  //.......
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.