将Wcf服务应用到Java脚本应用程序中

时间:2018-04-06 00:25:41

标签: javascript wcf ado.net

我正在将wcf服务用于java脚本Web应用程序。我通过使用类库项目创建了wcf服务,然后我添加了引用java脚本客户端项目。我想通过使用Web应用程序将新记录插入数据库,但问题是当我点击提交按钮时,它会在谷歌控制台窗口中显示以下错误..

**Uncaught ReferenceError: tempuri is not defined
at CallStudentDataService (Default.aspx:19)
at HTMLInputElement.onclick (Default.aspx:93)**

这是界面。

    [OperationContract]
    string InsertStudentRecord(string Name, string Email, string Address, string Mobile);

这是实施。

 public string InsertStudentRecord(string Name, string Email, string Address, string Mobile)
        {
            SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=3TierInWindowsApplication;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("AddNewStudent", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@Name", Name);
            cmd.Parameters.AddWithValue("@Address", Address);
            cmd.Parameters.AddWithValue("@EmailId", Email);
            cmd.Parameters.AddWithValue("@Mobile", Mobile);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            return "Success";
        }

这是.svc代码。

<%@ ServiceHost Language="C#" Debug="true" Service="StudentServiceWcf.StudentService"%>

这是HTML CODE。

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript">
        function CallStudentDataService() {
            var Name = document.getElementById("TxtName").value;
            var Address = document.getElementById("TxtAddress").value;
            var Email = document.getElementById("TxtEmail").value;
            var Mobile = document.getElementById("TxtMobile").value;

            tempuri.org.IService1.InsertStudentRecord(Name, Email, Address, Mobile, null, null);
        }

        function ShowMessage() {
            LblMessage.innerHTML = "Data Inserted Successfully";            
        }
    </script>

</head>
<body>  
    <form id="form1" runat="server">  
        <div>  
            <table style="height: 170px; width: 295px">  
                <tr>  
                    <td>Name:</td>  
                    <td>  
                        <input type="text" id="TxtName" /></td>  
                </tr>  
                <tr>  
                    <td>Address:</td>  
                    <td>  
                        <input type="text" id="TxtAddress" /></td>  
                </tr>  
                <tr>  
                    <td>Email:</td>  
                    <td>  
                        <input type="text" id="TxtEmail" /></td>  
                </tr>  
                <tr>  
                    <td>Mobile:</td>  
                    <td>  
                        <input type="text" id="TxtMobile" /></td>  
                </tr>
                <tr>  
                    <td>  
                        <input type="button" id="BtnSendDetails" value="Submit" onclick="CallStudentDataService()" /></td>  
                    <td>  
                        <label id="LblMessage"></label>  
                    </td>  
                </tr>  
            </table>  
            <asp:ScriptManager ID="ScriptManager1" runat="server">  
                <Services>  
                    <asp:ServiceReference Path="~/StudentService.svc" />  
                </Services>  
            </asp:ScriptManager>  
        </div>  
    </form>  
</body>  </html>

这是屏幕截图wsdl ..

enter image description here

这是我运行应用程序时的屏幕截图。

enter image description here

1 个答案:

答案 0 :(得分:0)

以下是我为解决此错误所做的更改。

  1. 我将.svc文件代码更改为&lt;%@ ServiceHost Language =&#34; C#&#34;调试=&#34;真&#34;服务=&#34; StudentServiceWcf.StudentService&#34;   厂=&#34; System.ServiceModel.Activation.WebScriptServiceHostFactory&#34; %GT;

  2. 要找到正确的界面路径,只需在localhost路径中添加带符号(/)的jsdebug,如http://localhost:51142/StudentService.svc/jsdebug