Mein Problem ist 405 (Methode nicht erlaubt), aber ich habe mein Problem nicht gelöst wo ist das Problem? Warum kann ich nicht ohne Parameter posten?POST ohne URL Parameter: 405 (Methode nicht erlaubt)
javascript hier Code
<script type="text/javascript">
$(document).ready(function() {
var getResult = GetData();
console.log("GetData => " + getResult);
var getResult2 = PostDataURLParamater();
console.log("PostDataURLParamater => " + getResult2);
var getResult3 = PostData();
console.log("PostData => " + getResult3);
})
function GetData() {
var result;
$.ajax({
url: "http://localhost:64073/api/home/helloworld",
type: "GET",
datatype: "JSON",
async: false,
success: function (data) {
result = data;
},
error: function (msg) {
console.log("Error: " + msg);
}
});
return result;
};
function PostDataURLParamater() {
var result;
$.ajax({
url: "http://localhost:64073/api/home/HelloWorldForPost?name=MustafaParamater",
type: "POST",
datatype: "JSON",
async: false,
success: function (data) {
result = data;
},
error: function (msg) {
console.log("Error: " + msg);
}
});
return result;
}
function PostData() {
var result;
var data = JSON.stringify({ name: "Mustafa" });
$.ajax({
type: 'POST',
url: 'http://localhost:64073/api/home/HelloWorldForPost',
data: data,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function (data) {
result = data;
},
error: function (msg) {
console.log("Error: ", msg);
}
});
return result;
}
</script>
und globale asax ist hier
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE,OPTIONS");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
}
}
und Web-api ist hier
public class HomeController : ApiController
{
public const string fv = "Hello Wold!";
[HttpGet]
public string HelloWorld()
{
return fv;
}
[HttpPost]
public string HelloWorldForPost(string name)
{
return string.IsNullOrEmpty(name) ? fv : (fv + " by " + name);
}
}
und Chromkonsole ist hier Chrome Console
http://encosia.com/ using-jquery-to-post-frombody-parameter-zu-web-api/ – granadaCoder