415不支持的媒体类型从$就调用WCF服务 [英] 415 Unsupported Media Type Calling WCF Service from $.ajax

查看:153
本文介绍了415不支持的媒体类型从$就调用WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个ASPX页面调用WCF Web服务,像这样:

I am attempting to call a WCF web service from an ASPX page like so:

var payload = {
    applicationKey: 40868578
};

$.ajax({
    url: "/Services/AjaxSupportService.svc/ReNotify",
    type: "POST",
    data: JSON.stringify(payload),
    contentType: "application/json",
    dataType: "json"
});

这样做会导致Web服务器返回错误 415不支持的媒体类型。我敢肯定,这是一个配置问题,其定义如下WCF服务:

Doing so results in the web server returning the error 415 Unsupported Media Type. I'm sure this is a configuration issue with the WCF service which is defined as follows:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]
void ReNotify(int applicationKey);

有在 web.config中没有条目文件,以便假定该服务使用默认配置。

There are no entries in the web.config file so assume that the service uses the default configuration.

推荐答案

我不是专家在此,其实我有同样的问题(还有另外一个原因)。然而,似乎WCF服务本身不支持AJAX,因此,您必须具备以下条件code。在你的web.config文件中启用它。

I am no expert in this, in fact I had the same problem (for another reason). However, it seems that WCF services don't inherently support AJAX and therefore you must have the following code in your web.config file to enable it.

<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="NAMESPACE.AjaxAspNetAjaxBehavior">
                <enableWebScript />
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
        multipleSiteBindingsEnabled="true" />
    <services>
        <service name="NAMESPACE.SERVICECLASS">
            <endpoint address="" behaviorConfiguration="NAMESPACE.AjaxAspNetAjaxBehavior"
                binding="webHttpBinding" contract="NAMESPACE.SERVICECLASS" />
        </service>
    </services>
</system.serviceModel>

和那么这在服务类

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 NAMESPACE
{
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class SERVICECLASS
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [OperationContract]
        public string DoWork()
        {
            // Add your operation implementation here
            return "Success";
        }

        // Add more operations here and mark them with [OperationContract]
    }
}

这是由VS 2012时产生我增加了一个AJAX的WCF服务。

This is what was generated by VS 2012 when I Added an AJAX enabled WCF service.

这篇关于415不支持的媒体类型从$就调用WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆