我有一个asp.net Web服务,我想通过Jquery Ajax调用但是在Executing我得到500 Internal Server Error
而在Response中我得到{"Message":"Invalid JSON primitive: Name.","StackTrace":"
看到Browser Developer工具中的输出(萤火虫)。
这是我的Webservice代码..
[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;
}
}
这是我的jquery Ajax代码..
$(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);
}
});
});
});
请帮我解决这个问题。谢谢......
答案 0 :(得分:1)
你可以尝试这样......
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetData(string Name, string Contact, string Email) {
return Name+Contact+Email;
}
$.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);
}
});
答案 1 :(得分:1)
试试这个
[WebMethod]
public string GetData(string Name, string Contact, string Email) {
return Name+Contact+Email;
}
将字符串值存储到局部变量,然后尝试如下
$.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);
}
});
答案 2 :(得分:0)
从你的ajax代码中删除以下行,它将起作用:
contentType: "application/json; charset=utf-8",
dataType: "json",