【问题标题】:why ajax call not succeed?为什么ajax调用不成功?
【发布时间】:2017-12-10 15:38:57
【问题描述】:

我创建了这个 ajax 函数:

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 () {

        }
    });
}

这是我的 web api 类:

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)
        {
        }
    }
}

每当触发 ajax 函数时,我都会收到此错误:

http://localhost/LayerProperty/get 404 (Not Found)

更新

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 }
            );
        } 

我的代码有什么问题?为什么 ajax 调用不成功?

【问题讨论】:

  • 你应该只通过 api/LayerProperty/5 而不是 api/LayerProperty/Get/5
  • crossDomain: true 你为什么用这个?是否需要在同一个域上模拟跨域请求?因为这就是此设置的作用。
  • 您能否也发布您的路由配置?
  • @juunas 请看更新
  • 我认为那是你的 MVC 路由而不是 WEB API...尝试发布一个 Web API...

标签: ajax asp.net-mvc asp.net-web-api


【解决方案1】:
function getAssociatedProperties(callback, error) {
    $.ajax({
        url: '/api/LayerProperty', // <--- You do not need the 'get' here, but you do need the /api/
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: 5,
        crossDomain: true,
        success: callback,
        error: function () {

        }
    });
}

【讨论】:

  • 米尔尼,感谢您的来信。但是当我使用您的示例时,我收到此错误:加载资源失败:服务器响应状态为 404(未找到)
【解决方案2】:

你能用下面的代码改变你的方法吗?

[HttpGet]
public IEnumerable<string> get()
{
            return new string[] { "value1", "value2" };
}

【讨论】:

    【解决方案3】:

    将控制器名称与您的控制器一起使用,然后它将从 ajax 请求中触发,目前您在 mvc 中没有使用正确的命名约定。

    你的

    public class LayerProperty : ApiController
    

    我的

    public class LayerPropertyController : ApiController 
    

    【讨论】:

      【解决方案4】:

      你的网址有误。

      function getAssociatedProperties(callback, error) {
          $.ajax({
              url: '/api/LayerProperty/get',
              type: "GET",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              data: 5,
              crossDomain: true,
              success: callback,
              error: function () {
      
              }
          });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-26
        • 1970-01-01
        • 2017-05-05
        • 2011-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多