ASP.NET:jQuery的AJAX'数据'参数问题 [英] ASP.NET: jQuery AJAX 'data' param problem

查看:151
本文介绍了ASP.NET:jQuery的AJAX'数据'参数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有问题,这个code,我花了过去三小时的周围挖,并试图找到答案。因为我没有成功,我将只是张贴了code,并要求对我的web服务,我应该有哪种类型的参数来处理这个请求:

 变参= [{关键:身份识别码,值:'myvalue的},{关键:myOtherId,值:myOtherValue'}]。
VAR dataToSend = {名称:fooId,值:fooValue'的args参数:args};
$阿贾克斯({
键入:POST,
网址:fooURL,
数据:dataToSend,
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON,
成功:的onSuccess,
错误:的OnError
});
 

现在,哪种签名我应该是能得到我的dataToSend?

我已经试过:

  [WebMethod的,ScriptMethod(ResponseFormat = ResponseFormat.Json)
公共字符串美孚(字符串名称,对象的值,列表和LT;参数数量>参数)
{
    返回确定;
}

公共类参数数量
{
    公共字符串键{获得;组; }
    公共对象值{获得;组; }
}
 

  [WebMethod的,ScriptMethod(ResponseFormat = ResponseFormat.Json)
公共字符串美孚(字符串名称,对象的值,对象[]参数)
{
    返回确定;
}
 

和也

  [WebMethod的,ScriptMethod(ResponseFormat = ResponseFormat.Json)
公共字符串美孚(dataToSend dataToSend)
{
    返回确定;
}

公共类dataToSend
{
    公共字符串名称{;组; }
    公共对象值{获得;组; }
    公开名单<参数数量>的args =新的名单,其中,参数数量>();

}
公共类参数数量
{
    公共字符串键{获得;组; }
    公共对象值{获得;组; }
}
 

解决方案

尝试传递数据作为一个字符串,而不是一个对象,即:

$阿贾克斯({
    ...
    数据:{A:2,B:3},
    ...
});

的推理是,如果你指定一个对象的数据,然后jQuery的序列化使用的查询字符串格式的数据,而服务器直接期待JSON格式。

此情况发生,尽管告诉jQuery来使用JSON作为数据类型 - 它似乎只涉及到的结果,而不是向服务器发送请求的有效载荷

一切你必须有正确的看向我。

I've been having problems with this code I had spent the last 3 hours digging around and trying to find an answer. As I wasn't successful, I will just post the code and ask which kind of parameters I should have on my web service to handle this request:

var args = [{ key: 'myId', value: 'myValue' }, { key: 'myOtherId', value: 'myOtherValue'}];
var dataToSend = { name: 'fooId', value: 'fooValue', args: args };
$.ajax({
type: 'POST',
url: 'fooURL',
data: dataToSend,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: OnSuccess,
error: OnError
});

Now, which kind of signature I should have to be able to get my "dataToSend"?

I've tried:

[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Foo(string name, object value, List<Args> args)
{
    return "OK";
}

public class Args
{
    public string key { get; set; }
    public object value { get; set; }
}

and

[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Foo(string name, object value, object[] args)
{
    return "OK";
}

and also

[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Foo(dataToSend dataToSend)
{
    return "OK";
}

public class dataToSend
{
    public string name { get; set; }
    public object value { get; set; }
    public List<Args> args = new List<Args>();

}
public class Args
{
    public string key { get; set; }
    public object value { get; set; }
}

解决方案

Try passing the data as a string, not an object, ie:

$.ajax( {
    ...
    data : '{ a: 2, b: 3 }',
    ...
} );

The reasoning for this is that if you specify an object as data then jQuery serializes the data using query string format, whereas the server is expecting JSON format directly.

This happens despite telling jQuery to use JSON as the data type - it seems to only relate to the result, not the request payload sent to the server.

Everything else you have there looks correct to me.

这篇关于ASP.NET:jQuery的AJAX'数据'参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆