缺少Ajax中的参数发布到Asmx

时间:2013-09-04 16:05:32

标签: javascript jquery ajax web-services asmx

我正在尝试将字符串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没有值。 我尝试了各种各样的东西,却无法理解我所缺少的东西?

1 个答案:

答案 0 :(得分:5)

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

data: { 'File': File },

或者作为字符串:

data: 'File=' + File,

目前你正在做一些不起作用的时刻。