【问题标题】:Cant able to access Webservice by using AJAX无法使用 AJAX 访问 Web 服务
【发布时间】:2014-10-17 04:06:57
【问题描述】:

我尝试使用以下 ajax 方法调用 webservice 方法。 但是我无法使用 AJAX 调用访问 Webservice 方法。Web 服务将在 ajax 成功中返回 JSON 字符串。

提前致谢。

<%@ 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>

【问题讨论】:

  • 控制台中的任何错误
  • @ArunPJohny got Bindgridview() is not defined 错误

标签: javascript c# jquery ajax web-services


【解决方案1】:

需要在Webservice.Before开始类中添加以下行[System.Web.Script.Services.ScriptService]

【讨论】:

    【解决方案2】:
    Please set type as below 
    

    类型:“POST”(大写字母)

    示例一

    <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
    

    还有这样的网络方法

    使用系统;
    使用 System.Collections.Generic;
    使用 System.Data;
    使用 System.Data.SqlClient;
    使用 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      相关资源
      最近更新 更多