I am a beginner in JQuery and I am trying to do a very simple thing: get a html form, convert it to JSON, send it to my API and display the result.
I read multiple SO post about serializing forms and arrays into JSON but I am unable to get it work(I either get 400 - Bad Request response because my JSON is not in a correct format or 415 status for some reason).
This is the Html form:
<form id="imageUploadForm">
        <fieldset>
        <legend>Upload new image</legend>
        <p>
                <label for="imageUrl">Image URL:</label>
                <input id="imageUrl" type="text" name="imageUrl" />
        </p>
        <p>
                <label for="tag">Tag:</label>
                <input id="tag" type="text" name="tag" />
        </p>
        <p>
                <input id="uploadButton" type="button" value="Submit" />
        </p>
        </fieldset>
</form>
<div id="uploadResponse"></div>
And script handling the request:
$(document).ready(function() {
  //Stops the submit request
  $("#imageUploadForm").submit(function(e) {
    e.preventDefault();
  });
  //checks for the button click event
  $("#uploadButton").click(function(e) {
        //get the form data and then serialize that
        var json = JSON.parse(JSON.stringify(jQuery('#imageUploadForm').serializeArray()));
        $.ajax({
        type: "POST",
        url: "<url>",
        data: json,
        crossDomain: true,
        dataType: "text/plain",
        //if received a response from the server
        success: function(response) {
                $("#uploadResponse").append(response);
        },
        });
  });
});
Could someone point me to the right direction? The goal is to send the following JSON to the api:
{
   "imageUrl" : "...",
   "tag" : "..."
}
 
     
     
     
     
     
     
     
    