【问题标题】:calling wcf service from jquery ajax - getting exception - incoming request is not allowed从 jquery ajax 调用 wcf 服务 - 获取异常 - 不允许传入请求
【发布时间】:2017-03-31 22:07:08
【问题描述】:

异常详情:

System.ServiceModel.Web.dll 中发生了“System.InvalidOperationException”类型的第一次机会异常 附加信息:不允许传入请求的 HTTP 方法“GET”(URI http://localhost:24342/Test.svc/DoWork?callback=jQuery110203769165082986856_1479380612393&testParameter=TEST&_=1479380612394)。

我正在使用 Ajax 嵌入式 WCF 服务。 WCF 客户端是一个 jQuery Ajax 调用。我的代码:

[ServiceContract(Namespace = "testWCFAjax")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{       
    [OperationContract]
    [System.ServiceModel.Web.WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,
       RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json )]         
    public string DoWork(string testParameter)
    {            
        return "success";
    }
}

网页配置:

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

Ajax 代码:

<script src="Scripts/jquery-1.9.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    function CallMyService() {
        $.support.cors = true;
        var param = '{"testParameter": "TEST"}';
        $.ajax({  
            url: 'http://localhost:24342/Test.svc/DoWork',
            data: param,
            type: "POST",
            dataType: "jsonp",                
            contentType: "application/json",                
            success: ServiceSucceeded,
            error: ServiceFailed
        });
    }

【问题讨论】:

  • 对于应该是var param = {"testParameter": "TEST"};(不带引号)的初学者。它会为你序列化匿名对象。
  • 嗨@GoneCoding - 我做到了,但仍然遇到同样的异常。

标签: jquery ajax wcf jsonp


【解决方案1】:

我能够通过使用 Ajax 嵌入式创建 WCF 服务来解决此问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-04
    • 2011-01-13
    • 2011-03-04
    • 2016-09-20
    • 2014-03-30
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多