question

Grant McNamara avatar image
Grant McNamara asked

I keep getting the BadRequest -- Invalid JSON in request error.

I keep getting the error but i dont think that there is something wrong with my code.

<!DOCTYPE html>
<html>
    <head>
      <title>That New Platformer Game</title> 
      <script src="https://download.playfab.com/PlayFabClientApi.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="style.css">
<script type="text/javascript">
            function Register(){
            // fetch title id
            var id = "97CAF";
            var displayname = document.getElementById("displayName");
            var username = document.getElementById("userName");
            var password = document.getElementById("passWord");
            var email = document.getElementById("email");
            if(!id || id == "")
            {
              OutputError("TitleId cannot be null");
              return;
            }
            else if(typeof PlayFab == 'undefined') // make sure we have the SDK prior to calling / setting 
            {
              OutputError("The PlayFab SDK could not be found. Double check your script sources");
              return;
            }

            // save these values locally to ease use
            localStorage.username = username;
            localStorage.displayname = displayname;
            localStorage.password = password;
            localStorage.titleId = id;
            
            // save title id
            PlayFab.settings.titleId = id;

            // build http request object for LoginWithCustomId
            var RegisterPlayFabUserRequest = {};
            RegisterPlayFabUserRequest.TitleId = id;
            RegisterPlayFabUserRequest.Email = email;
            RegisterPlayFabUserRequest.DisplayName = displayname;
            RegisterPlayFabUserRequest.Username = username;
            RegisterPlayFabUserRequest.Password = password;
            RegisterPlayFabUserRequest.RequireBothUsernameAndEmail  = true;

            OutputStatus("Registering...");
            PlayFabClientSDK.RegisterPlayFabUser(RegisterPlayFabUserRequest, (response, error) => {
                if(error)
                {
                  OutputError(error);
                } 
                else
                {
                  // display account details
                  var result = response.data;
                  var status = "Register Successful. <br \\> Your Id Is: " + result.PlayFabId + "<br \\> Your session ticket is: " + result.SessionTicket;
                  OutputStatus(status);
                }
            });
          };
     
</script>
</head>
<body>
<h1 class="headcenter">That New Platformer Game</h1>
<input id="displayName" type="text" name="displayName" placeholder="Enter a name that others will see">
<input id="userName" type="text" name="userName" placeholder="Enter a name to login">
<input id="passWord" type="text" name="passWord" placeholder="Password 6-100 characters">
<input id="email" type="text" name="email" placeholder="Email">
<button onclick="Register();">Register</button>

<div id="result"> </div>
<script type="text/javascript">
// some helper js methods 
          window.onload = function() 
          {
              var id = document.getElementById("titleId");

              if(!id)
              {
                OutputError("Please Fill Out The Form...");
                return;
              }
              else if(typeof PlayFab == 'undefined') // make sure we have the SDK prior to calling / setting 
              {
                OutputError("The PlayFab SDK could not be found. Double check your script sources");
                return;
              }
              id.value = localStorage.titleId ? localStorage.titleId : "";
          };

          function OutputError(error)
          {
            var output = document.getElementById("result");

            if(output)
            {
              output.style.color = "red";
              output.innerHTML = typeof error == "string" ? error : error.status + " -- " + error.errorMessage;  
            }
            else
            {
              console.error("#result could not be located.");
              console.error(error);
            }
          }

          function OutputStatus(request, error)
          {
            var output = document.getElementById("result");

            if(output)
            {
              output.style.color = "black";
              output.innerHTML = request;
            }
            else
            {
                console.log("#result could not be located.");
                console.log(request);
            }
          }
</script>
    </body>
</html>

Authentication
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gosen Gao avatar image Gosen Gao commented ·

Could you please output all variables read from web and make sure they match the required type?

0 Likes 0 ·

0 Answers

·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.