0

I used JQuery.Ajax to try to communicate with my webservice

Here is how the code looks like:

Main.onLoad = function() {
    // Enable key event processing
    this.enableKeys();
    widgetAPI.sendReadyEvent();
    //$("#h2Test").html("Change On Text");

        $.ajax({
                    url : 'http://---.--.---.--:-----/_layouts/-----/------.asmx?op=retrieveEvents',
                    type : "POST",
                    dataType : 'json',
                    contentType : 'application/json',
                    data : {
                        url : "someURL"
                    },
                    success : function(response) {
                        $("#h2Test").html("SUCCESS");
                    },
                    failure : function(response) {
                        $("#h2Test").html("FAIL");
                    }
        });
};

When i run the codes, Change On Text is displayed instead of SUCCESS or FAIL, why doesn't the code reaches either success or error

Cœur
  • 37,241
  • 25
  • 195
  • 267
Li Failure
  • 1
  • 2
  • 4
  • 1
    What is json.type? One probably reason could be a syntax error, your ajax not even got executed. Check your console. – PSL Oct 14 '13 at 03:24

2 Answers2

1

According to http://api.jquery.com/jQuery.ajax/ you should replace your

failure: function(response) {

with

error: function(response) {

It probably should reach the error function then.

Annihil
  • 151
  • 4
  • also remove the "contentType" thing. Are you sure your server expecting request "contentType" as "application/json"? The common web apis just use default POST content type as "application/x-www-form-urlencoded; charset=UTF-8" – imkrisna Oct 16 '13 at 07:45
0

In mobile and tv apps you should take care about "same domain policy" by using: dataType: "jsonp" and jsonpCallback (also declaring the latter). Take a look:

http://api.jquery.com/jQuery.ajax/

and

Callback function for JSONP with JQuery ajax

Community
  • 1
  • 1
Johann Echavarria
  • 9,695
  • 4
  • 26
  • 32
  • what is jsonp? and what does it do? – Li Failure Oct 14 '13 at 08:13
  • The browsers has a security concept: "same domain policy", it restrict your scripts to the same domain so your tv or mobile app will have that restriction because your webservice domain www.somedomain.com and your app domain (local) is different. jsonp is a technique that solve this and allow you to make cross-domain requests . See: http://stackoverflow.com/questions/12096867/same-domain-policy-jquery – Johann Echavarria Oct 14 '13 at 16:34
  • Very well explained here: http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about – Johann Echavarria Oct 14 '13 at 16:47