2015-06-25 33 views
0

我试图从本地机器上的跨域请求之下打电话。它调用但在服务器端,它无法在函数参数中找到传递的值。我正在使用MVC4 WebAPI。如何将数据从jquery发布到webapi(mvc4.0)?

$.ajax({ 
    url: 'http://localhost/webapp1/api/TicketAPI/GetTicketsByFilter', 
    type: 'POST', 
    dataType: 'jsonp', 
    data: { 
     Condition: '2', 
     StartDate: 's', 
     EndDate: 's',  
     Priority: '2', 
     Status: '2', 
     Category: 's', 
     PageNumber:1, 
     PageSize: 10, 
     OrderBy: 's', 
     OrderDir: 'asc' 
    }, 
    crossDomain: true, 
    contentType: "application/json; charset=utf-8", 
    success: function (data) { 
     debugger; 
     alert(data); 
     // result = JSON.parse(data.lstRecords); 
     // $(result).each(function (e) { 
     //  var p = new Date(this["CreatedOnUtc"]); 
     //  $("#list").addRowData(e, this); 
     // }); 
    }, 
    error: function() { 
     alert('fine'); 
    } 
}); 
} 
//below is the serverside code . 

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
//using System.Web.Mvc; 
using CS.Services.Tickets; 
using CS.Core; 
using System.Data.SqlClient; 
using System.Data; 
using CS.Services; 
using System.Web.Http; 
using CS.Web.Utility; 
using System.Security.Cryptography; 
using System.Configuration; 

public TicketSearchResult GetTicketsByFilter(SearchParameter ObjParameter) 
{ 
    //I always found ObjParameter is null .It should have some values 
    //some code here/ 
} 

如何ObjParameter提交值,使其具有值,而不是空?

public class SearchParameter 
    { 
     public SearchParameter() 
     { 
     PageNumber = 1; 
     PageSize = 100; 
     OrderBy = "TicketID"; 
     OrderDir = "ASC"; 
    } 

    public string Condition { get; set; } 
    public string StartDate { get; set; } 
    public string EndDate { get; set; } 
    public string Priority { get; set; } 
    public string Status { get; set; } 
    public string Category { get; set; } 
    public int PageNumber { get; set; } 
    public int PageSize { get; set; } 
    public string OrderBy { get; set; } 
    public string OrderDir { get; set; } 
} 
+0

使用JSONP'的'和'跨域:TRUE'是一个地域的要求有点奇怪。你可以将代码提供给'SearchParameter'类,所以我们可以看到你试图绑定的属性。 –

+0

从客户端发送到服务器的对象由框架绑定到操作参数。根据属性名称,它将转换该值。您正在初始化不兼容的数据,因此它会失败。例如startDate ='s'? – SBirthare

+0

添加了有问题的SearchParameter。 – skiskd

回答

0

您必须检查您的json对象是否具有与SearchParameter类相同的所有字段(以及相同类型)。

然后,您可以作出这样一个电话:

$.ajax({ 
     url: 'http://localhost/webapp1/api/TicketAPI/GetTicketsByFilter', 
     type: 'POST', 
     data:JSON.stringify({ Condition: '2', StartDate: 's', EndDate:'s',Priority: '2', Status: '2', Category: 's',PageNumber:1,PageSize:10,OrderBy:'s',OrderDir:'asc' }),    
     contentType: "application/json;charset=utf-8", 
     success: function (data) { 
      //your code 
     }, 
     error: function (x, y, z) { 
      alert(x + '\n' + y + '\n' + z); 
     } 
    }); 
+0

这与OP已经在做什么有什么不同?他的问题是,请求中发送的数据不会绑定到他的模型,也不是请求失败。 –

+0

当请求不被绑定到模型通常表示序列化的值不与反序列化目标类型MACHING。此外,我建议更改发布数据的方式,因此可以完成SearchParameter的反序列化。 – Luferogo

+0

@skiskd:您也缺少SearchParameter类的空白构造函数。你需要一个空白的构造函数。 – Luferogo