Question

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?

Was it helpful?

Solution

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);
          }
      });
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top