如何使用GET方法发送JSON对象

如何使用GET方法发送JSON对象

本文介绍了如何使用GET方法发送JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WebApi2控制器方法

My controller method with WebApi2

    [HttpGet]
    public IEnumerable<Products> GetProducts(ProductSearchCriteria searchCriteria)
    {
        //searchCriteria is always null here!!! Why?
        return db.Instance.LoadProducts(searchCriteria);
    }

我的搜索条件课程

public class ProductSearchCriteria
{
    private int id;
    private string name;
    private DateTime createdOn;

    [JsonProperty]
    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }

    [JsonProperty]
    public DateTime CreatedOn
    {
        get { return this.createdOn; }
        set { this.createdOn = value; }
    }

    [JsonProperty]
    public int ID
    {
        get { return this.id; }
        set { this.id = value; }
    }
}

我在html页面中的脚本

My script in the html page

<script>
    $("#btnTest").on("click", function () {
        var searchCriteria = {};
        searchCriteria.ID = 0;
        searchCriteria.Name = "";
        //searchCriteria.CreatedOn = "";
        var url = "http://localhost:8080/api/products"
        $.getJSON(url, searchCriteria).done(processResponse);
    });

    function processResponse(response){
    }
</script>

我到达了控制器方法(调试模式),但是ProductSearchCriteria searchCriteria参数始终为null.如何使用JQuery和WebApi2的get方法发送JSON对象?

I reach my controller method (debug mode) but the ProductSearchCriteria searchCriteria parameter is always null. How can I send my JSON object using get method with JQuery and WebApi2?

推荐答案

您正在使用$.getJSON(url, searchCriteria)getJSON 将searchCriteria发送为网址编码的查询字符串,因为您的searchCriteria符合普通对象

You are sending the query to the server using $.getJSON(url, searchCriteria) and getJSON sends the searchCriteria as a url-encoded query string because your searchCriteria would fit the definition of a plain object

在服务器端,.NET Web API的默认参数绑定将在URL中查找简单"数据类型(例如int,double,string),否则将退回到正文Content.

On the server side, the default parameter binding for .NET Web API will look in the URL for "simple" data types (e.g. int, double, string), otherwise it will fall back to the body Content.

要获得Web API模型绑定以从url中提取复杂类型,例如您的ProductSearchCriteria类,您需要在参数前面添加[FromUri]属性,如下所示:

To get Web API model binding to extract a complex type from the url, like your ProductSearchCriteria class you need to add the [FromUri] attribute in front of the parameter like this:

[HttpGet]
public IEnumerable<Products> GetProducts([FromUri] ProductSearchCriteria searchCriteria) {}

有关更多详细信息,请参见此处 ASP.NET Web API中的参数绑定

See here for more detail Parameter Binding in ASP.NET Web API

我认为值得尝试保留GET语义,而不是像某些人建议的那样切换到POST,因为您的代码正在有效地执行 read 操作,只要您不修改数据或状态... GET似乎都适用.

I would argue it is worth trying to preserve the GET semantics rather than switching to POST, as some have suggested, because your code is effectively doing what looks like a read operation and as long as you're not modifying data or state... a GET seems applicable.

这篇关于如何使用GET方法发送JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:23