Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 415不支持的媒体类型从$.ajax调用WCF服务_Jquery_Asp.net_Ajax_Wcf_C# 4.0 - Fatal编程技术网

Jquery 415不支持的媒体类型从$.ajax调用WCF服务

Jquery 415不支持的媒体类型从$.ajax调用WCF服务,jquery,asp.net,ajax,wcf,c#-4.0,Jquery,Asp.net,Ajax,Wcf,C# 4.0,我试图从ASPX页面调用WCF web服务,如下所示: var payload = { applicationKey: 40868578 }; $.ajax({ url: "/Services/AjaxSupportService.svc/ReNotify", type: "POST", data: JSON.stringify(payload), contentType: "application/json", dataType: "json"

我试图从ASPX页面调用WCF web服务,如下所示:

var payload = {
    applicationKey: 40868578
};

$.ajax({
    url: "/Services/AjaxSupportService.svc/ReNotify",
    type: "POST",
    data: JSON.stringify(payload),
    contentType: "application/json",
    dataType: "json"
});
这样做会导致web服务器返回错误
415不支持的媒体类型
。我确信这是WCF服务的配置问题,定义如下:

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

web.config
文件中没有条目,因此假设服务使用默认配置。

我不是这方面的专家,事实上我也有同样的问题(出于另一个原因)。但是,WCF服务似乎并不支持AJAX,因此您必须在web.config文件中包含以下代码才能启用它

<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>
这是VS2012在我添加支持AJAX的WCF服务时生成的

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]
    }
}