0

我已经在 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>
4

1 回答 1

0

我认为您需要在 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 "'}"
于 2013-07-25T10:43:34.337 回答