代码之家  ›  专栏  ›  技术社区  ›  Liam neesan

如何通过JSON将数组传递给asmxweb服务?

  •  0
  • Liam neesan  · 技术社区  · 6 年前

    我想把数组值传递给我的web服务文件。但它抛出以下错误

    设置删除文件

    试验

    测试窗体仅适用于以基元类型作为参数的方法。

    这是我的jquery代码

    $('#btnDeleteFiles').click(function () {
        var filesPaths = [];
        i = 0;
        $("input:checkbox[name=filed-checkedbox]:checked").each(function () {
            filesPaths[i] = $(this).val();
            i++;
        });
    
        //alert("filesPaths  =  " + filesPaths)
        var location = $('#ddlLocation option:selected').text();
        alert("Location = "+location)
        $.ajax({
            method: 'post',
            url: "GetAllFolderDetails.asmx/setDeleteFiles",
            data: {
                location: location,
                fileNames: filesPaths
            },
    
            dataType: "json",
            success: function (data) {
                window.location.reload(true);
                //alert("Success");
    
            },
            error: function (result) {
                alert("Error");
            }
        });
    });
    

    GetAllFolderDetails.asmx文件 代码

    [WebMethod]
    public void setDeleteFiles(string location, string[] fileNames)
    {
        var locationID = 0;
        var domain = "domain";
        var username = "xxx";   //username
        var Password = "***";    //password
    
        Debug.WriteLine("Location = "+location);
        Debug.WriteLine("fileNames = " + fileNames);
    
        using (new ImpersonateUser(username , domain, Password))
        {
            Debug.WriteLine("Files names = "+ fileNames);
            foreach (string file in fileNames)
            {
                FileInfo files = new FileInfo(file);
                files.Delete();
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Write(js.Serialize("Files are successfully Deleted"));
        }
    }
    

    注意

    如果我通过考试 string 作为一个没有数组的参数,它可以正常工作

    2 回复  |  直到 5 年前
        1
  •  0
  •   Chandramouli    6 年前

    使用以下命令

    data: JSON.stringify({ location: location, fileNames: filesPaths }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    

    并将输入参数数据类型更改为 List<string> 而不是 string[]

        2
  •  0
  •   Kevin Shah    6 年前

    把它拿走

     dataType: "json",
    

    从你的ajax电话。

    [System.Web.Script.Services.ScriptService]
    

    试试这个。