0

I wrote a WCF library and put it on a server (IIS7). I can reach its folders and its contents by browsing a URL like "foo.com/test.asmx" and its methods can be seen as "foo.com/test.asmx/MyMethod"

When I browse a page in the same application on server(servers's local) and send request to the method by using URL "foo.com/test.asmx/MyMethod" I get the result correctly.

But when I browse the same html page in my local computer and send the same request to the URL "foo.com/test.asmx/MyMethod" the ajax request fells in error state. (In addition to that, it works correctly on localhost in my computer)

I think I cannot reach the method or get results out of local server.

My ajax call:

var loginInfo = "{ 'username': '" + encodeURIComponent($("#lUsername").val()) + "', 'password': '" + encodeURIComponent($("#lPassword").val()) + "', 'uuid': 'asd'}";
$.ajax({
                type: "POST",
                //async: false,
                crossDomain: true,
                url: "http://foo.com/test.asmx/MyMethod",
                data: loginInfo,
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                success: function (msg) {                    
                    var myJsonObj = $.parseJSON(msg.d);                    
                    if (myJsonObj.userLoggedIn == 1) {                      
                        insertLoginToDB(myJsonObj.username, myJsonObj.userEmail, myJsonObj.userDeviceUuid);
                        $.mobile.changePage("#homePage", { transition: "none" });
                    }
                    else {
                        alert("Unsuccessful login. Try again.");
                    }                            
                },
                error: function (msg) {
                    alert("Error!");
                }
            });

How can I fix the problem? What causes this kind of error?

JoshuaJeanThree
  • 1,382
  • 2
  • 22
  • 41

1 Answers1

0

I am not sure if your client application or html page and your service both reside on different domains, but if they do, then it looks you are trying for cross domain posting using jquery (or plain javascript), for which below may look similar to your issue, see through it or google for "cross domain posting in jquery", i guess thats what is your core issue.

jQuery AJAX cross domain

in short if it's cross domain you r trying then you need to make use of JSONP datatype while making jquery ajax call.

Community
  • 1
  • 1
LearningNeverEnds
  • 374
  • 1
  • 3
  • 22