0

I have a form on my HTML page with some fields to be filled and an option to upload a file.
My Javascript function converts the inputs to the fom into a json file. I am trying to push this generated json along with the file uploaded by the user to a webservice but I am getting an error which says

405 OPTIONS

Here's the Ajax function I wrote. The formData.serializeObject() function gives me the Json output to the form.

$(function() {
$('form').submit(function() {
($('#file')[0].files[0].name);

            var formData = new FormData($("form")[0]);
            formData.append("filename", $('#file')[0].files[0].name);
            var obj = new FormData();
            form = form
            $.ajax({
                url: "Webserviceurl:port/function_to_send_json",
                type: "POST",
                data: JSON.stringify(formData.serializeObject()),
                dataType: "json",
                contentType: "application/x-www-form-urlencoded;charset=UTF-8",
                success: function(data) {
                    $.ajax({
                        url: "Webserviceurl:port/function_to_send_file",
                        type: "POST",
                        data: obj,
                        contentType: false,
                        success: function(data) {

                        },
                        error: function(data) {
                            console.log("Error Happened");
                        }
                    });
                    return false;
                },
                error: function(data) {
                    console.log("Error Happened");
                }
            });
        })
    });

What could I be doing wrong here?

thebjorn
  • 26,297
  • 11
  • 96
  • 138
Saksham Arora
  • 71
  • 2
  • 9

1 Answers1

0

The error message is being reported because you are making a preflighted request. This means the browser is sending an OPTIONS request asking for permission to make a POST request.

The server is responding to the OPTIONS request with a 405 error meaning "You can't make an OPTIONS request to this URL!"


That said …

data: JSON.stringify(…),

If you are sending JSON then you should say you are sending JSON, which ha a content type of application/json, not:

contentType: "application/x-www-form-urlencoded;charset=UTF-8",

There is no serializeObject method on formData objects, so formData.serializeObject() should be throwing an exception.


This question covers how to upload files with Ajax.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I have written my own function to convert the data to JSON format and its working. There's no error being thrown there. I even changed the content type to JSON and hosted my website and my webservice on the same Tomcat server but I'm still getting the exact same error. – Saksham Arora Dec 19 '16 at 13:12