2

I am calling goog.net.XhrIo from within this onclick event

goog.events.listen(invBtn, goog.ui.Component.EventType.ACTION,
        function(e) {

   goog.net.XhrIo.sent('http://remotehost.com:8080/customer/add');
 (update :typo here send instead of sent )

This is the very basic task I want to accomplish but this call doesn't even reach the server. I also tried passing url as goog.Uri , doesn't help. I wonder what is stopping from making this call to the server, I tried both host name and ip address but neither helps. It just does nothing.

Is there any thing I can do to see why this call fails to even reach the server.

Appreciate any help

regards Eddie

Eddie
  • 125
  • 1
  • 2
  • 8

1 Answers1

2

In your plovr config file, set the warning level to VERBOSE:

// config.js
{
  "paths": "js",
  "mode": "ADVANCED",
  "level": "VERBOSE"
}

With VERBOSE warnings enabled, running your program shows the following warning:

JSC_INEXISTENT_PROPERTY: Property sent never defined on goog.net.XhrIo ...
goog.net.XhrIo.sent('http://remotehost.com:8080/customer/add');
^

Try changing goog.net.XhrIo.sent(); to goog.net.XhrIo.send();.

In addition, you may want to pass a callback function to the XhrIo send function as follows:

goog.net.XhrIo.send('http://remotehost.com:8080/customer/add', function(e) {    
  var xhr = /** @type {goog.net.XhrIo} */ (e.target);
  alert(xhr.getResponseText());
});

Another common pattern is to create an xhr object and register an event listener:

var xhr = new goog.net.XhrIo();
goog.events.listenOnce(xhr, goog.net.EventType.COMPLETE, function(e) {
  var xhr = /** @type {goog.net.XhrIo} */ (e.target);
  alert(xhr.getResponseText());
  xhr.dispose(); // Dispose of the XHR if it is not going to be reused.
});
xhr.send('http://remotehost.com:8080/customer/add');

Further Reading

Closure: The Definitive Guide, Chapter 7: Client-Server Communication

Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • Thanks cpeisert. I tried it all. 'sent' was a typo here and I updated. I used send all the while and tried almost every possible goog.net. Only jsonp is actually calling the server. My problem is xhr call does not call the server and I found it out when I see the logs from the server. This is the actual call I made ( xhr.send(parUrl,"POST", goog.json.serialize(data), {'Content-Type':'application/json'}); )initially, but now after repeated fails all I want to see is xhr calling my server. Used settings you mentioned in Plovr and have been following the Michael Bolin's book.No luck yet. Thanks – Eddie Jul 04 '12 at 22:49
  • Did you try passing a callback function to test for `xhr.isSuccess()` and comparing `xhr.getLastErrorCode() == goog.net.ErrorCode.ABORT` and `xhr.getLastErrorCode() == goog.net.ErrorCode.TIMEOUT` as shown on page 158 of *Closure: The Definitive Guide*? – Christopher Peisert Jul 04 '12 at 23:02
  • Thanks again cpeisert. I tried using getLastErrorCode and it gave me result 0. I think its a cross domain issue. Otherwise it should not be so complicated to make a call. Or it could be something that is blocking call from within server. But I really appreciate your help. Best Regards.(I can't voteup because I don't have much credits yet, once I have , I will up vote your answer) – Eddie Jul 04 '12 at 23:20