Ajax调用WCF:错误请求

时间:2015-05-03 01:11:55

标签: jquery asp.net wcf asp.net-ajax

使用jquery从ASP.NET页面调用WCF方法时获取错误请求。我错过了一些简单的东西吗?

已编辑:

如果我调用无参数方法,它可以正常工作。一旦我尝试发送参数,我就会收到错误请求错误。

var d = JSON.stringify({ control: 'usercontrol1' });
        $.ajax({
            url: "http://localhost:51926/CLAWPage.svc/LoadUserControl",
            type: "POST",
            data: d,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                jQuery('#placeholder').append(response); // or response
                $(".portlet").draggable();
            },
            error: function (x, y, z) {
                jQuery('#placeholder').append(z);
            }
        });

Webservice方法:

    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
    string LoadUserControl(string control);

Webservice方法实现。

    public string LoadUserControl(string control)
    {
        using (Page _page = new Page())
        {
            //Load the user control
            UserControl usrControl = (UserControl)_page.LoadControl("Controls/" + control + ".ascx");
            //Set the name and age parameter values to User Control's respective labels
            _page.Controls.Add(usrControl);
            using (StringWriter writer = new StringWriter())
            {
                _page.Controls.Add(usrControl);
                HttpContext.Current.Server.Execute(_page, writer, false);
                return writer.ToString();
            }
        }
    }

0 个答案:

没有答案