【问题标题】:Calling Webservice asynchoronously using ajax使用ajax异步调用Web Service
【发布时间】:2013-07-25 10:36:34
【问题描述】:

我已经在 C# 中使用 asp.net 构建了一个 web 服务,并且正在运行 “http://ABC.com/WebSite1/Service.asmx”,我正在尝试使用 jquery ajax 异步调用从另一个 asp.net 网站页面使用此服务。但服务 url 没有被点击,我收到一条错误消息。

This is my webservice:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment   
the following line. 
[ScriptService]
public class Service : WebService
{
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void GetUserDetails(string Email, string StreetAddress, string City, string     State, string ZipCode, string Phone) {
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT * FROM OrderTb WHERE Email = '" + Email + "' AND StreetAddress = '" + StreetAddress + "' AND City = '" + City + "' AND State = '" + State +"' AND ZipCode = '" + ZipCode +"' AND Phone = '"+ Phone + "'", con);
    cmd.ExecuteNonQuery();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    // Create an instance of DataSet.
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
        }

}

这是我的 aspx 页面,包括 javascript:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"   Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Getting Data from WebService</title>
</head>
<script type="text/javascript" src="Scripts/jquery-1.8.3.js"></script>
<script>
    function GetShippingData() {
        var email = $("#txtEmail").val();
        var street = $("#txtStreetAddress").val();
        var city = $("#txtCity").val();
        var state = $("#txtState").val();
        var zipCode = $("#txtZipCode").val();
        var phone = $("#txtPhone").val();
        $.ajax(
        {
            type: 'POST',
            url: 'http://abc.com/WebSite1/Service.asmx/GetUserDetails',
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            async: true,
            data: { Email: email, StreetAddress: street, City: city, State: state,   ZipCode: zipCode, Phone: phone },
            success: function (result) {
                alert('success!');
            },
            error: function (result) {
                alert("Error");
            }
        });
    }
</script>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>
                        <b>Enter Email:</b>
                    </td>
                    <td>
                        <asp:TextBox ID="txtEmail"  runat="server">abc@gmail.com</asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>Enter StreetAddress:</b>
                    </td>
                    <td>
                        <asp:TextBox ID="txtStreetAddress" runat="server">70 abc  road</asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>Enter City:</b>
                    </td>
                    <td>
                        <asp:TextBox ID="txtCity" runat="server">Kolkata</asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>Enter State:</b>
                    </td>
                    <td>
                        <asp:TextBox ID="txtState" runat="server">ABC</asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>Enter ZipCode:</b>
                    </td>
                    <td>
                        <asp:TextBox ID="txtZipCode" runat="server">123232</asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>Enter Phone:</b>
                    </td>
                    <td>
                        <asp:TextBox ID="txtPhone" runat="server">1234567890</asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="btnSubmit" runat="server" Text="Submit"  OnClick="btnSubmit_Click" OnClientClick="GetShippingData();" />
                    </td>
                </tr>
            </table>
        </div>
        <div>
            <asp:GridView ID="gvUserDetails" runat="server" EmptyDataText="No Record Found">
                <RowStyle BackColor="#EFF3FB" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        </div>
    </form>
</body>
</html>

【问题讨论】:

    标签: jquery asp.net asp.net-mvc web-services


    【解决方案1】:

    我认为您需要在 web.config(在您的网络服务中)添加以下内容

     <system.web>
      <webServices>
        <protocols>
          <add name="HttpGet"/>
          <add name="HttpPost"/>
        </protocols>
      </webServices>
    

    如果这不是解决方案,您还可以提供您遇到的错误

    编辑:

    尝试在您的 ajax 调用中写入数据值,如下所示:

    data: "{ Email: '" + email + "', StreetAddress: '" + street + "', City: '" + city + "', State: '" + state + "',   ZipCode: '" + zipCode + "', Phone: '" + phone "'}"
    

    【讨论】:

    • 我已经尝试了 web.config 中的建议。但它没有用。我的主要问题是服务 URL 没有受到异步 ajax 调用的影响,并且我收到了我在 ajax 函数的错误属性中设置的错误消息。我在 Visual Studio 2010 的 ASP.net 开发服务器中执行此操作.
    • 好吧,它仍然没有说明错误是什么。我提供了另一种解决方案来解决我的一个问题。也许它会解决你的问题。但也许可以尝试检查错误回调返回的结果对象,看看是否找不到错误原因
    猜你喜欢
    • 2015-04-10
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多