2014-10-30 116 views
0

我有一个API控制器CustomerController返回客户的数据:JQuery的Ajax调用ASP.NET MVC API返回错误

using System.Collections.Generic; 
using System.Linq; 
using System.Web.Http; 
using CustomerMngmt.Models; 

namespace CustomerMngmt.Controllers 
{ 
    public class CustomersController : ApiController 
    { 
     private readonly Customer[] _customer = 
    { 
     new Customer 
     { 
      Id = 1, 
      firstName = "Florian", 
      lastName = "Hofmeister", 
      emailAddress = "[email protected]", 
      company = "Germany", 
      pictureURL = "......jpg" 
     }, 
     new Customer 
     { 
      Id = 2, 
      firstName = "Svenja", 
      lastName = "Hofmeister", 
      emailAddress = "[email protected]", 
      company = "Germany", 
      pictureURL = "......jpg" 
     }, 
     new Customer 
     { 
      Id = 3, 
      firstName = "Marvin", 
      lastName = "Hofmeister", 
      emailAddress = "[email protected]", 
      company = "Germany", 
      pictureURL = "......jpg" 
     } 
    }; 



     public IEnumerable<Customer> GetAllCustomers() 
     { 
      return _customer; 
     } 

     public Customer GetCustomerById(int id) 
     { 
      var customer = _customer.FirstOrDefault(c => c.Id == id); 

      return customer; 
     } 
    } 
} 

然后我在JavaScript加载数据有:

function loadCustomer() { 

    jQuery.support.cors = true; 
    $.ajax({ 
     url: 'http://localhost:54172/api/customers', 
     type: 'GET', 
     dataType: 'json', 
     data:{}, 
     success: function (data) { 

      for (var i = 0; i < data.length; i++) { 
       var customer = new customer(
        '1', 
        data[i].id, 
        data[i].firstName, 
        data[i].lastname, 
        data[i].companyName, 
        data[i].emailAddress, 
        data[i].pictureURL); 

       self.customers.push(customer); 
      } 
     }, 
     error: function (x, y, z) { 
      alert(x + '\n' + y + '\n' + z + "TEST"); 
      console.log(x + '\n' + y + '\n' + z + "TEST"); 
     } 
    }); 
} 

和我问题在于该功能不会从控制器加载元素。相反,我收到一条错误消息[object Object]errorTEST

+0

我看不出你的API有什么问题。尝试将'url'更改为'url:'/ api/customers''。 – Groyoh 2014-10-30 13:15:27

+0

是你的API调用会好吗? – 2014-10-30 13:16:05

+0

检查控制台中的网络选项卡以查看实际响应和错误。 – tymeJV 2014-10-30 13:16:58

回答

1

在阿贾克斯网址不符任何控制器或行动。如果脚本位于cshtml文件中,则可以使用url助手构建url。出于安全原因,如果您在此进行身份验证,我建议使用POST而不是GET。最后,你可以使用responseText来查看实际的错误信息。

$.ajax({ 
    url: @Url.Action("Customers", "GetAllCustomers"), // or try 'http://localhost:54172/Customers/GetAllCustomers' 
    type: 'POST', 
    success: function (data) { 
     for (var i = 0; i < data.length; i++) { 
      var customer = new customer(
       '1', 
       data[i].id, 
       data[i].firstName, 
       data[i].lastname, 
       data[i].companyName, 
       data[i].emailAddress, 
       data[i].pictureURL); 

      self.customers.push(customer); 
     } 
    }, 
    error: function (x, y, z) { 
     alert(x.responseText + '\n' + y + '\n' + z + "TEST"); 
     console.log(x.responseText + '\n' + y + '\n' + z + "TEST"); 
    } 
}); 
+0

不能proplem因为ie给了我可靠的数据 – 2014-10-31 08:11:23

0

尝试改变网址这一个 - 在我看来,很显然你应该改变一个AJAX网址(/控制器/动作),并从你的API控制器返回JSON> ControllerName /法

+0

不,它不起作用 – 2014-10-30 13:38:08

+0

您是否试图将客户数据显示到不同的视图或局部视图中?如果是这种情况,那么为什么不创建一个具有视图模型的部分视图来加载所有的东西呢? – frustratedprogrammer 2014-10-30 14:12:56

+0

没有iam写一个SPA一个html数据,所以我有htmldata的列表视图 – 2014-10-30 14:19:13

0

。因此,我看不到在您的代码中命名客户的操作。

+0

我不知道你的意思 – 2014-10-30 13:48:29

+0

@cassteam有没有需要一个名为客户的行动,也不需要返回JSON。 Web API自动将GET方法路由到以“Get”开头并基于Accept标头返回json的操作。检查[这里](http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api)和[here](http:// www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api) – Groyoh 2014-10-30 13:53:11

0

API没什么问题,我怀疑这是你的JavaScript在试图创建customer集合时炸毁了。尝试添加这给JavaScript:

function customer(id, firstname, lastname, companyname, email, pic) { 
      this.Id = id; 
      this.FirstName = firstname; 
      this.LastName = lastname; 
      this.Company = companyname; 
      this.email = email; 
      this.Pic = pic; 
     } 

,并改变你创建变量,我已经改变了在我的例子cus ...

var cus = new customer(
         '1', 
         data[i].id, 
         data[i].firstName, 
         data[i].lastname, 
         data[i].companyName, 
         data[i].emailAddress, 
         data[i].pictureURL); 

        self.customers.push(cus); 
+0

抱歉,但错误不会消失与此:( – 2014-10-30 14:59:33

相关问题