0

I am creating a billing form to send data to a jsonrpc endpoint. Though the server returns status code 200; i get this response {message: "Parse error. Invalid JSON was received by the server.", code: -32700,..}

    $(document).on("submit", "#billing", function(event)
{
    event.preventDefault();        
    $.ajax({
        url: $(this).attr("action"),
        type: $(this).attr("method"),
        dataType: "JSON",
        data: {new FormData(this)},
        processData: false,
        contentType: false,
        success: function (data, status)
        {
            console.log('Submission was successful.');
            console.log(data);
        },
        error: function (xhr, desc, err)
        {
            console.log('An error occurred.');
            console.log(data);
        }
    });        
});
<form name="billing" id="billing" action="https://XX.XX.XX.XX/ghe/api2/call/jsonrpc2" method="POST">
    First Name: <input type="text" id="firstname" name="firstname"/> <br/>
    Surname: <input type="text" id="surname" name="surname" /> 
<br/>
    Email : <input type="text" id="email" name="email"/> 
<br/>
    <input type="submit" name="submit" value="submit">
</form>

I saw somewhere to use stringify and tried that also via:

$("#submit").click(function()
{
 $("#billing").submit(function(e)
 {
  var postData = $(this).stringify();
  var formURL = $(this).attr("action");
  $.ajax(
  {
   url: $form.attr('action'),
   type: "POST",
   data : postData,
   success:function(data, textStatus, jqXHR) 
   {
    console.log("success");

   },
   error: function(jqXHR, textStatus, errorThrown) 
   {
    console.log("Failed");
   }
  });
 });
  
 $("#billing").submit(); //SUBMIT FORM
});

But i still get that same error response. What am i doing wrong? New to the use of APIs so my know how is kinda scattered now; would appreciate if can be pointed to a resource learning url best for APIs..

Thanks

Debaryoh
  • 73
  • 7

1 Answers1

1

You need to capture the payload that is being sent to the server to see what is causing the invalid Json-Rpc exception.

Your could console.log(postData). Or i'd suggest your browser debugger or fiddler.

Once you have captured the json that is being sent to the server you can drop it in a json linter jsonLint.com and see what's causing your trouble.

Austin Harris
  • 1,676
  • 1
  • 17
  • 22