2014-10-17 90 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