使用 Ajax 将 JSON 发送到 WCF 3.5

2024-04-20

我在将 JSON 传递给 Weight 方法时遇到问题。我不断得到HTTP/1.1 415 Cannot process the message because the content type 'application/x-www-form-urlencoded; charset=UTF-8' was not the expected type 'text/xml; charset=utf-8'.

我认为我的合同或 web.config 有问题。我所有的研究结果都是空的。我将使用 jQuery 的 $.ajax 从 Web 部件调用此服务。

界面:

namespace XXX.SharePoint.WebServices
{
    [ServiceContract]
    public interface ICalculators
    {

        [OperationContract]
        [WebInvoke(Method = "POST",
                   BodyStyle = WebMessageBodyStyle.WrappedRequest,
                   RequestFormat = WebMessageFormat.Json,
                   ResponseFormat = WebMessageFormat.Json
        )]
        Single Weight(Single Width, Single Diameter, Single Size, Single Factor);
    }
}

网络配置:

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="XXX.SharePoint.WebServices.CustomServiceBehaviour"
        name="XXX.SharePoint.WebServices.Calculators">
        <endpoint address=""
                  binding="basicHttpBinding"
                  contract="XXX.SharePoint.WebServices.ICalculators" >
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://moss2010/"></add>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="XXX.SharePoint.WebServices.CustomServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid
disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

一如既往,提前致谢!


以下是 IIS 中托管的 WCF 服务的完整工作示例:

[ServiceContract]
public interface ICalculators
{
    [OperationContract]
    [WebInvoke(Method = "POST",
               BodyStyle = WebMessageBodyStyle.Wrapped,
               RequestFormat = WebMessageFormat.Json,
               ResponseFormat = WebMessageFormat.Json
    )]
    float Weight(float width, float diameter, float size, float factor);
}

public class Calculators : ICalculators
{
    public float Weight(float width, float diameter, float size, float factor)
    {
        return 10f;
    }
}

calculators.svc:

<%@ 
    ServiceHost 
    Language="C#" 
    Debug="true" 
    Service="XXX.SharePoint.WebServices.Calculators" 
    Factory="System.ServiceModel.Activation.WebServiceHostFactory"
    CodeBehind="Calculators.svc.cs" 
%>

web.config:

<system.serviceModel>
    <services>
        <service 
            behaviorConfiguration="XXX.SharePoint.WebServices.CustomServiceBehaviour"
            name="XXX.SharePoint.WebServices.Calculators">
                <endpoint 
                    address=""
                    binding="webHttpBinding"
                    contract="XXX.SharePoint.WebServices.ICalculators"
                />
                <endpoint 
                    address="mex" 
                    binding="mexHttpBinding" 
                    contract="IMetadataExchange" 
                />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="XXX.SharePoint.WebServices.CustomServiceBehaviour">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>  

在同一 ASP.NET 应用程序中使用 jQuery 的消耗:

$.ajax({
    url: '/calculators.svc/Weight',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({ Width: 1.2, Diameter: 2.3, Size: 3.4, Factor: 4.5 }),
    success: function (result) {
        alert(result.WeightResult);
    }
});

注意使用webHttpBinding代替basicHttpBinding(SOAP) 在 web.config 以及特殊的WebServiceHostFactory用在.svc file.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Ajax 将 JSON 发送到 WCF 3.5 的相关文章

随机推荐

  • 使用偏移时导航栏项目可点击区域被剪切

    在 SwiftUI 中 考虑这个导航栏 这是尾随的方式 navigationBarItems宣布 HStack spacing 0 Button action self addListModal true label NavBarImage
  • Python - 对包含字符串和数字的列表中的数字值进行排序

    我创建了一个列表 其中包含 python 中分数文件的所有信息 分数 txt 文件 Dan Danson 9 6 1 John Johnson 5 7 10 Mike Mikeson 10 7 6 我这样做是为了将 txt 文件中的信息获取
  • ruby splat 运算符在多重赋值期间到底如何获取数组的第一个和其余部分?

    在 ruby 中 可以将多重赋值与 splat 运算符结合起来 以模拟函数式语言中的first 和rest 或head 和tail first rest 1 2 3 4 first output 1 rest output 2 3 4 sp
  • 有人使用 ASP .NET 会员资格吗?

    想知道是否有人使用 ASP NET 会员资格 如果你这样做 请告诉我你的想法 如果您不使用它 您打算在未来的项目中使用它吗 如果没有 为什么 RWendi 是的 已经用过很多次了 它会为你节省很多工作 客户经常会要求更改用户名和管理密码重置
  • 如何使用适用于 C 和 C++ 的 GSOAP 访问 Amazon AWS S3?

    我到处搜索这个 但找不到一个合适的代码 我怎样才能访问亚马逊 AWS S3 http aws amazon com s3 服务使用GSOAP http gsoap2 sourceforge net 下面的代码来自OP 最初 该帖子包含问题和
  • VS 2012 扩展中的标准控件

    我目前正在更改公司内部的 VS 扩展以支持 Visual Studio 2012 我正在努力解决的是如何使 UI 动态适应活动的 VS 主题 我找到了几个颜色 画笔的资源键 Microsoft VisualStudio Shell 11 0
  • Google Freebase API 如何获取图像的 URL?

    所以 我试图弄清楚如何获取 Freebase 数据库中图像的 URL 我想要一张旧金山的照片 这就是我获取旧金山主题的方式 https www googleapis com freebase v1sandbox topic 2Fen 2Fs
  • 编译具有多个参数的案例类会导致 java.lang.StackOverflowError

    我有以下带有大量参数 150 的案例类 在使用 sbt 版本 0 14 编译期间导致 java lang StackOverflowError 异常 类定义 case class TestClass Param1 String Param2
  • 通过 url 将整数传递给 php $_GET

    所以我正在做一些 CTF 挑战之一是关于 php 类型杂耍 代码看起来像这样 if GET var1 hash md4 GET var1 print flag 所以我 80 确定我需要传入一个整数 这样它就会是真的 但我能操作的只是 url
  • jQuery Uncaught TypeError 与 Theme Punch Revolution Slider

    我遇到了一个无法追踪的问题 我正在使用革命滑块 http themes themepunch com theme revolution jq我不断收到 jQuery 错误 Uncaught TypeError Cannot read pro
  • WordPress REST API - 允许任何人发帖

    我正在 WordPress 上构建一个应用程序 它需要一个简单的前端表单 任何人都可以提交需要保存在数据库中的信息 我正在尝试通过 REST API 来处理这个问题 由于应用程序的性质 提交此信息时不能进行任何页面重定向 我可以毫无问题地设
  • LinqDataSource:过滤并绑定到gridview

    问题没有按照我想要的方式解决 但我继续感谢 ukaszW pl 他的时间和努力 我正在使用 gridview 控件和 linqdatasource 它一切正常 我添加了 searchBySubject 的功能 并且添加了WhereParam
  • 如何将“旧的 spring mvc 控制器与 jsp”和“vaadin7 ui”集成在一起

    我正在尝试将 vaadin 与我的 spring mvc 应用程序集成 我有一些带有 jsp 文件的 url spring mvc 控制器使用它们 例如 mysite com spring mysite com spring example
  • Zend 引擎可以嵌入 PHP 之外吗?

    如果我记得的话 Zend 引擎的原始设计之一是它相对容易嵌入人们可能希望创建的其他语言 基本上 PHP 语法没有所有 PHP 模块 现在还是这样吗 嗯 Zend 引擎基本上是一个解释 PHP 字节码的虚拟机 基本上 您要做的就是为一种语言和
  • 为什么这个 css 动画无限自动播放轮播会在项目重置时跳转?

    我正在努力根据此处的示例创建无限自动播放轮播 https codepen io jackoliver pen qVbQqW https codepen io jackoliver pen qVbQqW 请注意 codepen 示例是多么流畅
  • java jar 的清单属性

    在您的帮助下 我完成了我的第一个 Java 项目 现在我想创建一个 jar 并从 jar 运行应用程序 Java 项目 它是一个普通的控制台应用程序 它有另一个项目 控制台应用程序 作为依赖项 我通过右键单击 导出 创建一个 jar 使用
  • 使用 C# 运行 T4 模板

    我有 T4 模板 mycode tt 它生成一个 cs 文件 我通常右键单击 tt 文件并选择 RunCustomTool 它会在内部获取 xml 文件并为我生成代码 现在我想使用 C Windows 应用程序运行自定义工具 因此 单击按钮
  • 如果您无法控制类,如何模拟类中的方法?

    我使用 Xunit 和 Moq 进行单元测试 到目前为止 我能够成功地模拟和测试接口中的方法 但是我应该如何模拟和测试我无法控制的类的方法 该类没有接口 方法也不是虚拟的 我研究了 Type Mock Isolator 但我无法使其工作 而
  • 订购 ActiveRecord 关系对象

    我有一个名为的 ActiveRecord 对象contact 它有一个关系叫做profiles 这些配置文件具有 url 属性 配置文件应按 url 按字母顺序排序 我试过了sort by也order但我收到此错误 contact prof
  • 使用 Ajax 将 JSON 发送到 WCF 3.5

    我在将 JSON 传递给 Weight 方法时遇到问题 我不断得到HTTP 1 1 415 Cannot process the message because the content type application x www form