WCF休息和angularjs 400 BAD在发布期间请求错误 [英] wcf rest and angularjs 400 BAD request error during post
问题描述
在按钮上单击,此功能称为
On button click this function is called
$scope.CreateNewTopic = function () {
var userdata = {
"TopicName":$scope.topicname,
"TopicDescription": $scope.topicdescription,
"OriginalPosterID": $scope.userid,
"CategoryID": $scope.selectedcategory.CategoryID
};
DataService.InsertTopic(userdata);
};
我的Angular Service方法
My Angular Service method
angular.module('DFApp').factory('DataService', function ($http, $log) {
var connectionurl = 'http://localhost:8020/DFServices.svc/'
return {
InsertTopic: function (topic) {
$http({
method: 'POST',
url: connectionurl + 'InsertNewTopic',
contentType: "application/json; charset=utf-8",
data: topic,
})
.success(function (data, status, headers, config) {
// successcallback(data);
console.log(data);
})
.error(function (data, status, headers, config) {
$log.warn(data, status, headers, config);
})
},
}
});
WCF方法
public int InsertNewTopic(InsertNewTopic InsertTopicObject) {
return SqlHelper.ExecuteNonQuery(SqlConnectionString.GetConnection(), CommandType.StoredProcedure, Constants.DF_InsertNewTopic.ToString(), new SqlParameter("TopicName", InsertTopicObject.TopicName), new SqlParameter("TopicDescription", InsertTopicObject.TopicDescription), new SqlParameter("OriginalPosterID", InsertTopicObject.OriginalPosterID), new SqlParameter("CategoryID", InsertTopicObject.CategoryID));
}
对应界面
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/InsertNewTopic", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
int InsertNewTopic(InsertNewTopic InsertTopicObject);
InsertNewTopic是如下所示的数据合同
InsertNewTopic is Datacontract as below
[DataContract]
public class InsertNewTopic
{
[DataMember]
public string TopicName { get; set; }
[DataMember]
public string TopicDescription { get; set; }
[DataMember]
public string OriginalPosterID { get; set; }
[DataMember]
public int CategoryID { get; set; }
}
Web配置文件
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" maxRequestLength="2147483647" />
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
<system.serviceModel>
<services>
<service name="DF.ServiceApi.DFServices" behaviorConfiguration="webHttpBehaviour">
<endpoint binding="webHttpBinding" contract="DF.ServiceApi.IDFServices" behaviorConfiguration="webEndPointBehaviour"
bindingConfiguration="webDFBinding"></endpoint>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webEndPointBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="webHttpBehaviour">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceThrottling maxConcurrentCalls="100" maxConcurrentInstances="10" maxConcurrentSessions="5" />
<serviceTimeouts transactionTimeout="00:20:00" />
<!-- 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="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webDFBinding" closeTimeout="00:20:10" openTimeout="00:20:00" sendTimeout="00:20:00" receiveTimeout="00:20:00" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Streamed" maxBufferSize="2147483647"
contentTypeMapper=""
>
<readerQuotas maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="2147483647" maxDepth="32" maxStringContentLength="8192" />
<security mode="None">
</security>
<!--<security mode="Transport">
<transport clientCredentialType="None" />
</security>-->
</binding>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" closeIdleServicesAtLowMemory="true" multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<!--<webHttpEndpoint>
<standardEndpoint name="" contentTypeMapper="Microsoft.Samples.WebContentTypeMapper.JsonContentTypeMapper, JsonContentTypeMapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</webHttpEndpoint>-->
</standardEndpoints>
<protocolMapping>
<add scheme="http" binding=""/>
</protocolMapping>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<!-- Inserted below code for enabling Webservices to allow CORS - Cross Origin Resource Sharing -->
<httpProtocol>
<customHeaders>
<!--<add name="Access-Control-Allow-Origin" value="*"/>-->
</customHeaders>
</httpProtocol>
</system.webServer>
该图像显示了我在开发人员工具的网络调用中的错误.在此处输入图像描述为了运行我的Web服务,我正在使用WebMatrix.我希望有人能帮助我,因为我的日子糟透了.
The image shows my error as in the network call of the developer tools. enter image description here And to run my webservices I am using WebMatrix. I wish some one to help me as my days are wasted like a hell.
推荐答案
这是一个CORS问题,应从WCF服务端解决此问题,
It's a CORS issue, which should be fixed from the WCF service side,
首先,您必须在web.config上启用CORS
First you must enable CORS on web.config
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE,OPTIONS" />
</customHeaders>
</httpProtocol>
OPTIONS请求,您必须以空响应答复,并添加您的应用程序类:
OPTIONS requests you must reply with empty response, adding in your application class:
protected void Application_BeginRequest()
{
if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
{
Response.Flush();
}
}
这篇关于WCF休息和angularjs 400 BAD在发布期间请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!