xiaolongit11
2021-10-15 00:30
采纳率: 0%
浏览 28

Jquery Ajax 调用WCF服务方法,老是提示错误信息404,不知道错到哪里?

运行工具:VS2019

  1. AjaxService.svc 服务代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

namespace WCF_Demo
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    //js前端访问服务,必须添加
    [JavascriptCallbackBehavior(UrlParameterName = "jsoncallback")] 
    public class AjaxService
    {
        // 要使用 HTTP GET,请添加 [WebGet] 特性。(默认 ResponseFormat 为 WebMessageFormat.Json)
        // 要创建返回 XML 的操作,
        //     请添加 [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     并在操作正文中包括以下行:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [OperationContract]
        [WebGet(ResponseFormat =WebMessageFormat.Json)]
        public string GetFristService()
        {
            // 在此处添加操作实现
            return "hello,这个是我的第一个WCF服务";
        }

        // 在此处添加更多操作并使用 [OperationContract] 标记它们
    }
}

2.HtmlPage1.html ,前端调用


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src="JS/jquery-1.4.1.min.js"></script>
    <script type="text/javascript">

        $(document).ready(function () {

            $("#btnSubmit1").click(function () {

                $.getJSON("https://localhost:44394/AjaxService.svc/GetFristService?jsoncallback=?", function (data) {

                    alert(data);
                    var vData = JSON.stringify(data);
                    alert(vData);
                });

            });
            

            $("#btnSubmit2").click(function () {
                $.ajax({
                    type: "get",
                    dataType: "json",
                    url: "https://localhost:44394/AjaxService.svc/GetFristService?jsoncallback=?",
                    success: function (returndata) {
                        $("#content").html(returndata);
                    }
                });

            });

        });

    </script>

</head>
<body>
    <div id="content"></div>
    <input id="btnSubmit1" type="button" value="调用方法1" />
    <input id="btnSubmit2" type="button" value="调用方法2" />
</body>
</html>

3 . web.config 配置文件


<?xml version="1.0" encoding="utf-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
        <authentication mode="Forms"></authentication>
    </system.web>
    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
            <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
        </compilers>
    </system.codedom>

    <system.serviceModel>
        <bindings>
            <webHttpBinding>
                <binding name="AjaxServiceBind" crossDomainScriptAccessEnabled="true" />
            </webHttpBinding>
        </bindings>

        <behaviors>
            <endpointBehaviors>
                <behavior name="WCF_Demo.AjaxServiceAspNetAjaxBehavior">
                    <enableWebScript />
                </behavior>
            </endpointBehaviors>

            <serviceBehaviors>
                <behavior name="" > <!--解决当前已禁用此服务的元数据发布-->
                    <serviceMetadata httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>

        </behaviors>

        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

        <services>
            <service name="WCF_Demo.AjaxService">
                <endpoint address="" behaviorConfiguration="WCF_Demo.AjaxServiceAspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="AjaxServiceBind" name="AjaxService" contract="WCF_Demo.AjaxService" />
            </service>
        </services>
    </system.serviceModel>

</configuration> 

注意: 这是运行服务的url, 显示发布成功的。

img

最后运行点击调用按钮,提示下面错误信息:

img

img

找了好久都不知道错到哪里,求各位赐教,有经验的朋友麻烦帮我看下,谢谢了

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • img

    服务的端口号是 1606 协议是 http
    ajax请求 的地址写成了https://localhost:44394/
    端口号写成了 44394 协议写成了 https

    打赏 评论
  • xiaolongit11 2021-10-15 00:35

    大侠们,搞定了,调用的url地址没有写对。我眼瞎了,无语了,网页上面都写了调用地址的:http://localhost:1606/AjaxService.svc/GetFristService?jsoncallback=?

    打赏 评论

相关推荐 更多相似问题