簡體   English   中英

將Wcf服務消費到Java腳本應用程序中

[英]Consuming Wcf Service into Java Script Application

我在Java腳本Web應用程序中使用了wcf服務。 我通過使用類庫項目創建了wcf服務,然后添加了參考Java腳本客戶端項目。 我想使用Web應用程序將新記錄插入數據庫,但是問題是當我單擊“提交”按鈕時,它在Google控制台窗口中顯示以下錯誤。

**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代碼。

<%@ 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的屏幕截圖..

在此處輸入圖片說明

這是我運行該應用程序時的屏幕截圖。

在此處輸入圖片說明

這是我為解決此錯誤所做的更改。

  1. 我將.svc文件代碼更改為<%@ ServiceHost Language =“ C#” Debug =“ true” Service =“ StudentServiceWcf.StudentService” Factory =“ System.ServiceModel.Activation.WebScriptServiceHostFactory”%>

  2. 要找到正確的接口路徑,只需在本地主機路徑(如http:// localhost:51142 / StudentService.svc / jsdebug )中添加帶符號(/)的jsdebug

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM