【问题标题】:Missing parameter in Ajax post to AsmxAjax post 到 Asmx 中缺少参数
【发布时间】:2013-09-04 16:05:31
【问题描述】:

我正在尝试将字符串 File 发送到我的 asmx 服务,但我不断收到以下错误:

    Message: Invalid web service call, missing value for parameter: File
    StackTrace   
at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters) at 
System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\\r\\n   at 
System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\\r\\n   at 
System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)\",\"ExceptionType\":\"System.InvalidOperationException\"}

这是 JS

function AJAXActionPostData(service, operation, File, callback, async)
{
    if (!async) { async = false; }
    $.ajax({
        type: 'POST',
        url: '/API/Service.asmx/operation',
        contentType: 'application/json; charset=utf-8',
        async: async,
        data: "{ 'File': '" + File + "' }",
        dataType: 'json',
        success: function (msg) { if (msg) { callback(msg.d); } },
        error: ErrorHandler
    });
}

当传递给上面的函数file 的值为“test\r\n”时,转义字符会不会弄乱它?

服务代码

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public bool UploadCSV(string id, string File)
    {
        string testfile = File;
        return true;
    }

没有抛出其他错误,只是 File 没有值。 我尝试了各种方法,但无法理解我缺少什么?

【问题讨论】:

    标签: javascript jquery ajax web-services asmx


    【解决方案1】:

    尝试将data 作为普通对象发送:

    data: { 'File': File },
    

    或者作为字符串:

    data: 'File=' + File,
    

    目前你正在做这两种方法都行不通的。

    【讨论】:

    • 著名@Rory McCrossan +1 :)
    • 感谢您的快速回复! "data: {'File': File}" 抛出无效的 JSON 原始异常
    • "data: 'File=' + File" 与数据类型文本完美配合!我猜我的 JSON 格式不正确或引用不正确,谢谢
    • @Rory 我也遇到了同样的错误,但是我有三个变量,我要不要用'&'连接它们
    猜你喜欢
    • 2012-09-21
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2018-04-23
    • 2015-07-02
    相关资源
    最近更新 更多