Ich schaffe diese Ajax-Funktion:Warum Ajax-Aufruf nicht erfolgreich?
function getAssociatedProperties(callback, error) {
$.ajax({
url: '/LayerProperty/get',
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: 5,
crossDomain: true,
success: callback,
error: function() {
}
});
}
Und hier ist meine Web-api Klasse:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace GeomindMobile.Controllers
{
public class LayerProperty : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5
public string Get(int id)
{
return "value";
}
// POST api/<controller>
public void Post([FromBody]string value)
{
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
}
Jedes Mal, wenn Ajax-Funktion feuerte ich diesen Fehler:
http://localhost/LayerProperty/get 404 (Not Found)
aktualisieren
Here is my RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Was ist falsch an meinem Code, warum Ajax-Aufruf nicht erfolgreich ist?
Sie sollten nur api/LayerProperty/5 nicht api/LayerProperty/Get/5 – Milney
'domänenübergreif passieren: true', warum diese verwenden? Müssen Sie eine crossDomain-Anfrage für dieselbe Domain simulieren? Weil das ist, was diese Einstellung tut. – ADyson
Können Sie Ihre Routing-Konfiguration auch posten? – juunas