0

I'm trying to develop an application on phonegap on Android.

What I'm trying to do is call a webservice from my application, this will be used for data access.

I found many sheets online on how to do this, and the simplest one I could find that seems straight forward goes like this

<script type="text/javascript">
 $(document).ready(function() {
  $.ajax({
type: "POST",
url: "http://www.webservicex.net/stockquote.asmx/GetQuote",
data: "{'usd'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {

  // Insert the returned HTML into the <div>.
  $('#Content').html(msg.d);
}
 });
});

However, All I can see is the text I had already put into the div, the div contents do not change. I'm testing on a Galaxy SIII device directly, not on a simulator.

Nicolas R
  • 13,812
  • 2
  • 28
  • 57
KamalSalem
  • 495
  • 2
  • 8
  • 21

1 Answers1

1

My guess is that nothing seems to happen because you are getting a fail, not a success. try adding an error handler and see what happens.

.fail(function() { alert("error"); })

You are likely having 'cross domain' issues (because you are trying to get ajax from a different domain) and may need to use JSONP instead of JSON for your dataType. For more info about JSONP, see this post or the docs

EDIT:

Here is a fiddle of this code in action.

$(document).ready(function() {

//here is one way using the 'error' handler
    $.ajax({
        type: "POST",
        url: "/failOnPurpose",
        data: "{'usd'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error:function(jqXHR, textStatus) { 
            alert("error:" + textStatus); 
        },
        success: function(msg) {        
          // Insert the returned HTML into the <div>.
          $('#Content').html(msg.d);
        }    
    });

//here is another way using the 'fail' request handler
    $.ajax({
        type: "POST",
        url: "/failOnPurpose",
        data: "{'usd'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",            
        success: function(msg) {        
          // Insert the returned HTML into the <div>.
          $('#Content').html(msg.d);
        }    
    }).fail(function(jqXHR, textStatus) {
              alert( "fail: " + textStatus );
    });

});​
Community
  • 1
  • 1
davehale23
  • 4,374
  • 2
  • 27
  • 40
  • being the rookie that I am, I am unaware where to place this function, can you please direct me in the right direction there? I see a success: tag, does it come after it? – KamalSalem Oct 20 '12 at 21:41