sanatan mishra sanatan mishra - 5 months ago 29
Ajax Question

Issue in Calling WebService by jquery Ajax

I have a an asp.net Webservice which i want to call by Jquery Ajax but on Executing i am getting

500 Internal Server Error
and in the Response i am getting
{"Message":"Invalid JSON primitive: Name.","StackTrace":"
on seeing the output in Browser Developer tool(Firebug).
Here is my Webservice code..

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class DateWebService : System.Web.Services.WebService {

HttpRequest request;

public DateWebService () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string GetData(string name, string contact, string email) {

string Name = request[name];
string Contact = request[contact];
string Email = request[email];

return Name+Contact+Email;
}
}


and here is my jquery Ajax Code..

$(document).ready(function(){

$("#Button1").click(function() {


$.ajax({
type: "POST",
url: "/ASPNET_WebServices_JQuery/DateWebService.asmx/GetData",
data: { 'Name': 'SRI', 'Contact': '787979879898', 'Email': 'hr@.com' },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg)
{
$("#output").text(msg.d);
}
});
});

});


Please help me to resolve this issue..Thanks...

Answer

try this

[WebMethod]
public   string GetData(string Name, string Contact, string Email) {

               return Name+Contact+Email;
}  

Store your string values to local variables then try like below

 $.ajax({
                    type: "POST",
                    url: "/ASPNET_WebServices_JQuery/DateWebService.asmx/GetData",
                    data: '{"Name":"' + Name + '","Contact":"' + Contact + '","Email":"' + Email + '"}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg)
                    {
                        $("#output").text(msg.d);
                    }
                });