1

I encountered some weird error by doing some short-polling ajax()-request to a play2.1-server.

Currently I am using REST to send a request to the server and I await a json as answer. The server always response the correct JsonP but after a short time the client gets a "parsererror" and stops calling the ajax-callbackmethod for all following requests.

The Client:

   function restGet(url, callback) {
       $.ajax({
          type: 'GET',
          url: 'www. ... /getQuestions/42',
          dataType: 'jsonp',
          jsonpCallback: 'callbackMethod',
          success: 'callbackMethod',
          error: function (jqXHR, status, exception) {
              console.log('jqXHR: ' + JSON.stringify(jqXHR));
              console.log('restGet error: ' + status + ' - ' + exception);
          }
      });
   }


  function callbackMethod(response) {
       console.log('At callbackMethod(' + JSON.stringify(response) + ')');

  }

The Server:

    public static Result getQuestions(String lectureId) {
        String callbackMethod = request().getQueryString("callback");
        String json = "{\"question\":\"Do you find my error?\"}";
        return ok((callback == null)?json:callback + "("+ json + ")");
    }

According to Fiddler Web Debugger the server always sends the same (and correct) jsonp-string. And the restGet-Method from the client is called every second.

The client prints out the following as soon as it crashes:

[17:46:24.036] jqXHR: {"readyState":4,"status":200,"statusText":"success"}

[17:46:24.036] restGet error: parsererror - Error: callbackMethod was not called

I don't know what's wrong with my code and the other posts I found about parsererror always said you have to use jsonp instead of json. That's what I did, didn't I?

4444
  • 3,541
  • 10
  • 32
  • 43

1 Answers1

0

Function names should not be quoted.

   function restGet(url, callback) {
       $.ajax({
          type: 'GET',
          url: 'www. ... /getQuestions/42',
          dataType: 'jsonp',
          jsonpCallback: 'callbackMethod',
          success: callbackMethod,
          error: function (jqXHR, status, exception) {
              console.log('jqXHR: ' + JSON.stringify(jqXHR));
              console.log('restGet error: ' + status + ' - ' + exception);
          }
      });
   }
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `success` should be a function, not a string, but `jsonpCallback` can be a string. From the doc: "Specify the callback function name for a JSONP request... you can also use a function for this setting, in which case the value of jsonpCallback is set to the return value of that function." http://api.jquery.com/jQuery.ajax/ – Jason P Jul 15 '13 at 15:55
  • @JasonP Thanks, fixed that. Although I suspect he shouldn't be using the same callback function for both purposes. Usually you just let jQuery use its internal JSONP callback function. – Barmar Jul 15 '13 at 15:59
  • Does it help if you don't use `jsonpCallback`? – Barmar Jul 15 '13 at 16:31
  • Wow, I don't really know why, but it works after I dropped the "jsonpCallback". Thanks a lot. – user2584032 Jul 15 '13 at 16:44
  • Because your callback function returns `undefined`, not the object. – Barmar Jul 15 '13 at 17:47