-1

I am submitting my form data in JSON format to a third-party server using an AJAX call, but I'm getting a 405 error (method not allowed). Could you please help?

My form looks like this:

<form name="freeTrial" id="testForm" role="form" >
------------
<input type="submit" class="btn" value="Submit" id="submitButton"/>
</form>

And the script I am using is:

$(document).ready(function(){
    alert("page load");
    $("#submitButton").click(function(e){
        e.preventDefault()
        alert("inside function");
        var MyForm = $("#testForm").serializeJSON();
        alert("MyForm "+JSON.stringify(MyForm));
        jQuery.ajax({
            url : "https://myServerURl/accounts",
            type: 'POST',
            dataType: 'jsonp',
            data : MyForm,
            success:function(data){
                alert(data);
            },
            error: function(jqXHR, textStatus, errorThrown){
                console.log("failure");
                alert("failure");
            }
        });
    });
});
Alexander
  • 9,737
  • 4
  • 53
  • 59
  • But this error tells you everything that you need to know. Have you got defined in your controllers request mapping /accounts for POST request? And can you debug, what URL you are calling on submit button click? – Beri Jan 30 '15 at 13:04
  • Secondly, you should add this event on form submit, not on button click. – Beri Jan 30 '15 at 13:04
  • jsonp doesn't support POST (as it is implemented by making a GET request using a `script` tag) - did you mean `dataType: 'json'`? – Sean Vieira Jan 30 '15 at 13:07
  • @Sean: jsonp required for CORS request and json for same domain. – Vaibhav J Jan 30 '15 at 13:09
  • Maybe these links could help [http://stackoverflow.com/q/2558977/2456894](http://stackoverflow.com/q/2558977/2456894) , [http://stackoverflow.com/q/3506208/2456894](http://stackoverflow.com/q/3506208/2456894) – Icaro Martins Jan 30 '15 at 13:11
  • @Vaibhav - JSON CORS requests works just fine, as long as you are not using IE 8 / 9 - jQuery checks the target url's domain and [does the right thing if it doesn't match the requested one](https://github.com/jquery/jquery/blob/e0673dfedb9ad07d8e68f28a54453b975c412c33/src/ajax.js#L507-L525). – Sean Vieira Jan 30 '15 at 13:23

2 Answers2

0

Your code is actually attempting to make a Cross-domain (CORS) request, not an ordinary POST. Modern browsers allows only Ajax calls to services in the same domain as the HTML page.

How to solve it?

The simplest way is to enable CORS (enable the necessary headers) on the server.

If you don't have server-side access to it, you can mirror the web service from somewhere else, and then enable CORS there.

Vaibhav J
  • 1,316
  • 8
  • 15
0

You cannot use jsonp with POST, it just doesn't work. If this link targets same application you are calling from - use json data type.

If not, you must on target Server enable (by applying specific heards) Cross Domain requests. This can be done only inside https://myServerURl server. Than you could use simply json data type, instead of jsonp.

Related post: How to use type: "POST" in jsonp ajax call

Community
  • 1
  • 1
Beri
  • 11,470
  • 4
  • 35
  • 57