2014-11-24 82 views
1

我有一个asp.net Web服务,我想通过Jquery Ajax调用,但执行我得到500 Internal Server Error和响应我得到{"Message":"Invalid JSON primitive: Name.","StackTrace":"看到在浏览器开发工具的输出(萤火虫)。 这里是我的web服务代码..问题通过jQuery的Ajax调用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': '[email protected]' }, 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (msg) 
       { 
        $("#output").text(msg.d); 
       } 
      }); 
     }); 

    }); 

请帮我解决这个issue..Thanks ...

回答

1

试试这个

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

       return Name+Contact+Email; 
} 

Store中的字符串值,局部变量,然后尝试像下面

$.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); 
        } 
       }); 
+0

它不工作在所有。 – 2014-11-24 12:12:32

+0

检查已编辑的答案.... – 2014-11-24 12:17:48

+0

'未知的网络方法GetData'错误放在'静态' – 2014-11-24 12:23:06

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': '[email protected]' }", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (msg) 
       { 
        $("#output").text(msg.d); 
       } 
      }); 
0

删除以下行从您的Ajax代码,它会工作:

contentType: "application/json; charset=utf-8", 
 
dataType: "json",