2014-10-17 89 views
0

我嘗試使用以下ajax方法調用webservice方法。 但我無法使用AJAX調用訪問Webservice方法。webservice將以ajax成功返回JSON字符串。無法使用AJAX訪問Webservice

在此先感謝。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 

    <script src="jquery-1.11.0.min.js"></script> 

    <script type="text/javascript"> 
     $(document).ready(function() { 
      alert('invoke1') 
      $("#testbtn").click(function() { 
       alert('btnclick') 
       $.ajax({ 
        type: "Post", 
        url: "WebService.asmx/GetAllRecords", 
        data: "{}", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: function (data) { 

         var Employees = data.d; 
         $('#grddata').empty(); 
         for (var i = 0; i < Employees.length; i++) { 
          if (i == 0) { 
           $('#grddata').append('<table><tr><td><strong>Emp_Title:</strong></td><td>' + Employees[i] + '</td></tr>'); 
          } 
          else if (i % 2) { 
           $('#grddata').append('<tr><td><strong> Emp_Name:</strong> </td><td>' + Employees[i] + '</td></tr>'); 
          } 
          else { 
           $('#grddata').append('<table><tr><td><strong>Emp_Title:</strong></td><td>' + Employees[i] + '</td></tr>'); 
          } 

         } 
        }, 
        failure: function (data) { 
         alert("Error Ha..Ha...Ha..."); 
        } 
       }); 


      }) 
     }); 
    </script> 
</head> 

<body> 
    <form id="form1" runat="server"> 
     <input type="button" onclick="BindGridView()" id="testbtn"/> 
     <div id="grddata"> 
     </div> 
    </form> 
</body> 

</html> 
+0

任何錯誤在控制檯 – 2014-10-17 04:12:10

+0

@ArunPJohny了Bindgridview()沒有定義錯誤 – User 2014-10-17 04:16:34

回答

0

需要添加以下行Webservice.Before啓動類[System.Web.Script.Services.ScriptService]

0
Please set type as below 

類型: 「POST」(大寫)

實施例1

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
    $.ajax({ 
    type: "POST", 
    contentType: "application/json; charset=utf-8", 
    url: "Default.aspx/BindDatatoDropdown", 
    data: "{}", 
    dataType: "json", 
    success: function(data) { 
    $.each(data.d, function(key, value) { 
    $("#ddlCountry").append($("<option></option>").val(value.CountryId).html(value.CountryName)); 
    }); 
    }, 
    error: function(result) { 
    alert("Error"); 
    } 
    }); 
    }); 
    </script> 
enter code here 

和Web方法如使用本系統

;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;

[WebMethod] 
public static CountryDetails[] BindDatatoDropdown() 
{ 
DataTable dt = new DataTable(); 
List<CountryDetails> details = new List<CountryDetails>(); 

using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true")) 
{ 
using (SqlCommand cmd = new SqlCommand("SELECT CountryID,CountryName FROM Country", con)) 
{ 
con.Open(); 
SqlDataAdapter da = new SqlDataAdapter(cmd); 
da.Fill(dt); 
foreach (DataRow dtrow in dt.Rows) 
{ 
CountryDetails country = new CountryDetails(); 
country.CountryId = Convert.ToInt32(dtrow["CountryId"].ToString()); 
country.CountryName = dtrow["CountryName"].ToString(); 
details.Add(country); 
} 
} 
} 
return details.ToArray(); 
} 

參考鏈接

http://www.aspdotnet-suresh.com/2012/07/how-to-bind-dropdownlist-in-aspnet.html http://www.codeproject.com/Tips/810571/Calling-Server-Side-Method-and-Web-Service-method http://www.aspdotnet-suresh.com/2013/12/call-wcf-service-from-jquery-ajax-json-example-aspnet.html