0

I have a simple function that uses $.Ajax. It's working fine when I deploy the this page to my online website, but on my dev environment it doesn't work, but there is no error. I tried both $.Ajax and $.Post. both not working, but in the F12 profiler tool i see the traffic.

Any Idea? Thanks.

f12

<script >
function RunAjax() 
{
    alert("before post");

        $.post("http://myshulmgr.com/GetData.asmx/GetEventMembers", { PID: "32", EventID: "8" },
       function (data) {
           alert("Data Loaded: " + data);
       });

        $.ajax({
            type: "POST",
            url: "http://myshulmgr.com/GetData.asmx/GetEventMembers",
            //data: {PID: iPID, EventID: iEventID},
            data: "{'PID': '" + 32
                    + "','EventID': '" + 8 + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",               
            success: function (jsonRes) 
            {
                alert(jsonRes);
            },
            failure: function (msg) 
            {
                alert(msg);
            }
        });

}

Itay.B
  • 3,991
  • 14
  • 61
  • 96

2 Answers2

2

Look at your browser's error console. There should be an informative message there.

Your dev environment is probably not on the same domain as the endpoint you are calling, so the same-origin policy is preventing your script from retrieving results from it. Since this is supposed to be a dev environment, you should probably set up a copy of the GetData.asmx script in that environment, rather than calling the production endpoints, and reference it as /GetData.asmx (i.e, as a domain-relative path) in your script.

1

You cannot perform cross-domain request because of same origin policy.

In your console you can see that this is actually the source of your problem:
console says (Aborted), giving a CORS preflight initiatior, i.e you tried to perform an cross-domain request, but it was aborted due to security reasons.

The reason why it works on production and not on dev is because probably your production environment is in the same domain (myshulmgr.com) to which the request is addressed, while dev environment is not.

Michał Rybak
  • 8,648
  • 3
  • 42
  • 54
  • How do you explain it working when i wrap it as a mobile app, using PhoneGap and running it from my android phone? It's not the same domain... – Itay.B Dec 28 '13 at 20:37
  • I have no experience with PhoneGap at all, but why do you say that domain is not the same there? Note that *domain* here is used in the meaning *unique name resolved to IP address of server*. – Michał Rybak Dec 28 '13 at 20:42
  • I see. I'm actually new to PhoneGap as well. So the conclusion is I can't debug my code? :) – Itay.B Dec 28 '13 at 20:45
  • If you want your app to work on dev environment, follow @duskwuff advice to make a local copy of `GetData.asmx`. – Michał Rybak Dec 28 '13 at 20:50
  • If there is now way to allow it, then I guess that's what i'll have to do. What if I didn't have the code? It bugs me. – Itay.B Dec 28 '13 at 20:53
  • Without the code you won't be able to do anything, probably, and there is nothing wrong with that. This is for security reasons. Refer to other sources (like [this question](http://stackoverflow.com/questions/5383045/why-cross-domain-ajax-call-is-not-allowed)), if you want to learn more about why such requests are not allowed. – Michał Rybak Dec 28 '13 at 20:57