使用ajax异步调用Web服务

时间:2013-07-25 10:36:35

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

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

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>

1 个答案:

答案 0 :(得分: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 "'}"