Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ServiceStack获取JSON?_Json_Rest_<img Src="//i.stack.imgur.com/WM7S8.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">servicestack - Fatal编程技术网 servicestack,Json,Rest,servicestack" /> servicestack,Json,Rest,servicestack" />

使用ServiceStack获取JSON?

使用ServiceStack获取JSON?,json,rest,servicestack,Json,Rest,servicestack,服务器端: [RestService("/x")] public class XFilter { public long[] CountryIds { get; set; } } public class XService : RestServiceBase<XFilter> { private const int PageCount = 20; public override object OnGet(XFilter request) {

服务器端:

[RestService("/x")]
public class XFilter
{
    public long[] CountryIds { get; set; }
}

public class XService : RestServiceBase<XFilter>
{
    private const int PageCount = 20;

    public override object OnGet(XFilter request)
    {
        Debugger.Break(); // request is always default at this point  !!
        return a;
    }
}
[RestService(“/x”)]
公共类XFilter
{
公共长[]countryID{get;set;}
}
公共类XService:RestServiceBase
{
私有常量int PageCount=20;
公共覆盖对象OnGet(XFilter请求)
{
Debugger.Break();//此时请求始终是默认的!!
返回a;
}
}
客户端:

<script type="text/javascript">

 var requestData= "{CountryIds:[1,2]}";
$.getJSON("/api/x", requestData, function (b) {

});

var requestData=“{countryId:[1,2]}”;
$.getJSON(“/api/x”),请求数据,函数(b){
});

这一定很容易,但我无法用这种方法在服务器端使用XFilter

var requestData= "{CountryIds:[1,2]}";
一定是

var requestData= {CountryIds:[1,2]};
!

但是getJSON中仍然存在一个问题。它不适用于数组

它的解决方案是:)

添加以下内容:

 $.ajaxSetup({
    traditional: true
});
一定是

var requestData= {CountryIds:[1,2]};
!

但是getJSON中仍然存在一个问题。它不适用于数组

它的解决方案是:)

添加以下内容:

 $.ajaxSetup({
    traditional: true
});

这是无效的JSON:

var requestData= "{CountryIds:[1,2]}";
必须引用所有字符串,例如:

var requestData= '{"CountryIds":[1,2]}';
var requestData = JSON.stringify({CountryIds:[1,2]}); //same as above

这将是通过代码> > $Ajax < /Cord> POST发送的,您可能会考虑,因为在GET请求中没有处理复杂类型的约定,因为它必须序列化到QuielStudio。

如果您想将复杂类型作为查询字符串发送,则需要自己构造URL。ServiceStack使用its来处理GET请求中的复杂类型(这只是带有CSV转义的JSON,即没有引号的.JSON)

因此,您现在的请求是:

var requestData = "[1,2]";
$.getJSON("/api/x?CountryIds=" + requestData, function (b) {
});

Re Cap:如果您想发送JSON,您需要通过ajax帖子发送。这不是有效的JSON:

var requestData= "{CountryIds:[1,2]}";
必须引用所有字符串,例如:

var requestData= '{"CountryIds":[1,2]}';
var requestData = JSON.stringify({CountryIds:[1,2]}); //same as above

这将是通过代码> > $Ajax < /Cord> POST发送的,您可能会考虑,因为在GET请求中没有处理复杂类型的约定,因为它必须序列化到QuielStudio。

如果您想将复杂类型作为查询字符串发送,则需要自己构造URL。ServiceStack使用its来处理GET请求中的复杂类型(这只是带有CSV转义的JSON,即没有引号的.JSON)

因此,您现在的请求是:

var requestData = "[1,2]";
$.getJSON("/api/x?CountryIds=" + requestData, function (b) {
});

Re Cap:如果你想发送JSON,你需要通过一篇ajax帖子来发送。

没有。我已经添加了回复。请检查一下。在javascript.No中必须使用它而不使用引号。我增加了回应。请检查一下。它必须在javascript中不带引号的情况下使用。