无法使用AJAX访问Web服务

时间:2014-10-17 04:06:58

标签: javascript c# jquery ajax web-services

我尝试使用以下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>

2 个答案:

答案 0 :(得分:0)

需要在Webservice.Before Starting Class中添加以下行 [System.Web.Script.Services.ScriptService]

答案 1 :(得分:0)

Please set type as below 

输入:&#34; POST&#34; (大写字母)

示例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方法一样

使用System;
    使用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