【问题标题】:unable to pass parameter in ajax url无法在ajax url中传递参数
【发布时间】:2014-06-13 04:09:02
【问题描述】:

我无法使用 jQuery url 将参数传递给 wcf 服务方法。

以下是我的服务合同:

namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "/InsertData/?Name={Name}&Email={Email}&Category={Category}&Mobile={Mobile}&Message={Message}")]
        string InsertData(string Name, string Email, string Category, string Mobile, string Message);
    }   
}

我的 jQuery 代码:

$(document).ready(function (e) {
        $('#BtnRegister').click(function (e) {
            alert("hi");
            debugger;
            var name = document.getElementById('TxtUserName').value;
            var email = document.getElementById('TxtUserEmail').value;
            var category = document.getElementById('TxtUserCategory').value;
            var mobile = document.getElementById('TxtUserMobile').value;
            var message = document.getElementById('message').value;

            $.ajax({
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                url: 'http://localhost:22727/Service1.svc/InsertData/',
                data: { Name: UserName, Email: UserEmail, Category: UserCategory, Mobile: UserMobile, Message: UserMessage },
                async: false,
                success: function (response) {
                    alert("Record Has been Saved in Database");
                },
                error: function ()
                { console.log('there is some error'); }
            });
            e.preventDefault();
        });
    });

我无法调用服务方法来传递参数。请帮忙。

【问题讨论】:

    标签: jquery html ajax wcf


    【解决方案1】:

    伊皮。我解决了我的问题。

    我已通过以下网址:

    url: 'http://localhost:22727/Service1.svc/InsertData/' + name + '/' + email + '/' + category + '/' + mobile + '/' + message,
    

    【讨论】:

      【解决方案2】:

      使用JSON.stringify序列化数据后尝试:

       $.ajax({
                      type: 'POST',
                      contentType: "application/json; charset=utf-8",
                      url: 'http://localhost:22727/Service1.svc/InsertData/',
                      data: JSON.stringify({ Name: UserName, Email: UserEmail, Category: UserCategory, Mobile: UserMobile, Message: UserMessage }),
                      async: false,
                      success: function (response) {
                          alert("Record Has been Saved in Database");
                      },
                      error: function ()
                      { console.log('there is some error'); }
                  });
      

      【讨论】:

        【解决方案3】:
        { Name: UserName, Email: UserEmail, Category: UserCategory, Mobile: UserMobile, Message: UserMessage }
        

        这里 UserName 是未定义的变量。使用您在如下代码中声明的变量

        { Name: name, Email: email, Category: category, Mobile: mobile, Message: message }
        

        希望一切顺利

        【讨论】:

          猜你喜欢
          • 2021-04-17
          • 1970-01-01
          • 1970-01-01
          • 2017-04-28
          • 2017-11-08
          • 2019-02-22
          • 1970-01-01
          • 2021-05-20
          • 1970-01-01
          相关资源
          最近更新 更多