xBoady 2021-10-15 00:30 采纳率: 66.7%
浏览 42
已结题

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月15日
  • 已采纳回答 11月7日
  • 创建了问题 10月15日

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料