1

I've created a game that collects a User's time on the browser with JavaScript. What I'd like to do is grab that JavaScript data and send it to the backend, which is written in Java (I'm using a local server running on Tomcat). How can I accomplish this?

I've looked into AJAX and here is what I've come up with...

          var myTime = // however long it took for user to win game

          var data = {}
          data["myTime"] = myTime;

          $.ajax({
              type : "POST",
              url : "/path-to/hosting/save",
              data : JSON.stringify(data),
              dataType : 'json',
              timeout : 100000,
              contentType:'application/json',
              success : function(data) {
                  console.log("SUCCESS: ", data);
              },
              error : function(e) {
                  console.log("ERROR: ", e);
              },
              done : function(e) {
                  console.log("DONE");
              }
          });

When I finish the game, I receive this error on the console: statusText:"parsererror"

My initial thought was that I didn't form my JSON correctly, but I am not sure. Any help would be appreciated. Thanks in advance!

2
  • What is the data type you use in Java to get the data send from the Javascript Commented Oct 29, 2017 at 2:50
  • I've been using this as a reference: mkyong.com/spring-mvc/… I created the same path to receive the data. I also created the HostingForm class to contain my variable myTime. Commented Oct 29, 2017 at 2:54

1 Answer 1

1

As your code it seems like myTime is a single value why you store it in array you can pass that as follows

var myTime = // however long it took for user to win game

      //var data = {}
      //data["myTime"] = myTime;

      $.ajax({
          type : "POST",
          url : "/path-to/hosting/save",
          data : JSON.stringify({
            'myTime': myTime                
          }),
          dataType : 'json',
          timeout : 100000,
          contentType:'application/json',
          success : function(data) {
              console.log("SUCCESS: ", data);
          },
          error : function(e) {
              console.log("ERROR: ", e);
          },
          done : function(e) {
              console.log("DONE");
          }
      });
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Nisal. The data type that JavaScript gives me is a "Number". I've tried changing my data type on the backend as a "float" and "double", but there is still a parsing error. Here is the response: {"timestamp":1509248487870,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required double parameter 'myTime' is not present","path":"/path-to/hosting/save"}
I see. We forgot to add a key for the value. However, I am now receiving this error in my console: ERROR {readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …} Per our setup above, this means that the ajax call was not a success. Do you have any other idea or tip about what the issue may be?
Actually, it does work now. I wrote some System.print.out lines and I am able to grab the data. Thank you!
Anytime hope you will be able to fix the server side problem as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.