Home > Blockchain >  AJAX Post Test Method Failed to load resource
AJAX Post Test Method Failed to load resource

Time:10-27

I created a test web application that is calling my MVC application. I can call the POST fine with Postman and wanted to try and call it from a web browser but I am getting a 405 error. ANy help would be great.

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

Test HTML Project

$.ajax({  
type: "POST",  
url: "URL",  
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
contentType: "application/json; charset=utf-8",  
dataType: "jsonp", 
crossDomain: true,
success: function (response) {  
   console.log('message: '   "success"   JSON.stringify(json));   
                 },  
failure: function (error) {  
   console.log('message Error'   JSON.stringify(error));
   }  
});  

MVC Application

public class PublicController : ApiController
    {
        [System.Web.Http.HttpPost]
        public IHttpActionResult MethodTest(string key, string action)
        {
        }
     }

web.config

<httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
      </customHeaders>
    </httpProtocol>

CodePudding user response:

you have to fix your ajax.remove headers and contenttype and fix dataType and add data

$.ajax({  
type: "POST",  
url: "http://localhost:60876/WebApi/public/SkipAuthenticationAndLogin"  
data:{
key:"key",
userName:"name",
succesUrl:"url"
},
dataType: "json", 
success: function (response) {  
   console.log('message: '   "success"   JSON.stringify(response));   
                 },  
failure: function (error) {  
   console.log('message Error'   JSON.stringify(error));
   }  
});  

and remove post from action


 public IHttpActionResult MethodTest(string key, string userName, string successUrl)
 {
}
  • Related