user3736324
user3736324

Reputation:

unable to pass parameter in ajax url

I am unable to pass the parameter to wcf service method using jQuery url.

Following is my service contract:

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);
    }   
}

My jQuery code:

$(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();
        });
    });

I am unable to call the service method to pass parameters. Please help.

Upvotes: 2

Views: 772

Answers (3)

user3736324
user3736324

Reputation:

Yippee. I Solved my problem.

I have passed the following URL:

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

Upvotes: 2

arpan sharma
arpan sharma

Reputation: 64

{ Name: UserName, Email: UserEmail, Category: UserCategory, Mobile: UserMobile, Message: UserMessage }

Here UserName is undefined variable. Use the variables which you have declared in code like below

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

Hope it will work fine

Upvotes: 0

Pranav Singh
Pranav Singh

Reputation: 20091

Try after serializing data using 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'); }
            });

Upvotes: 0

Related Questions