2014-11-24 94 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",