gpt4 book ai didi

json - 内容类型 'application/json; charset=utf-8' 不是预期的类型 'text/xml; charset=utf-8'

转载 作者:行者123 更新时间:2023-12-04 13:23:22 25 4
gpt4 key购买 nike

使用 firebug 时,我在我的 asp.net mvc 4 项目中收到此连线错误“NetworkError: 415 Cannot process the ...xt/xml; charset=utf-8'. - http://localhost:59899/wsccc1/wscccService.svc/RunTts”。

编码:

<script lang="javascript" type="text/javascript">
function ttsFunction() {
serviceUrl = "http://localhost:59899/wsccc1/wscccService.svc/RunTts";
var data = new Object();
data.text = $('#speak').val();
var jsonString = JSON.stringify(data);
$.ajax({
type: 'POST',
url: serviceUrl,
data: jsonString,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function() { alert('ok')},
error: function (xhr,status,error) {
console.log("Status: " + status);
console.log("Error: " + error);
console.log("xhr: " + xhr.readyState);
},
statusCode: {
404: function() {
console.log('page not found');
}
}
});
}
</script>

服务代码:
 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class wservice:Iwservice
{
public string RunTts(string value)
{
return value.ToUpper();
}
}

界面是:
namespace service
{
[ServiceContract]
public interface Iwservice
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "RunTts")]
string RunTts(string text);
}
}

和 web 配置,我在 WCF 中使用了无文件。:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment >
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory"
relativeAddress="./wsccc1/wscccService.svc"
service="service.wservice"/>
</serviceActivations>
</serviceHostingEnvironment>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

最佳答案

您需要使用 WebServiceHostFactory , 而不是常规的 ServiceHostFactory在你的代码中。这将使用适当的绑定(bind) ( webHttpBinding ) 和行为 ( webHttp ) 配置端点以遵守 [WebInvoke]属性。

<serviceHostingEnvironment >
<serviceActivations>
<add factory="System.ServiceModel.Activation.WebServiceHostFactory"
relativeAddress="./wsccc1/wscccService.svc"
service="service.wservice"/>
</serviceActivations>
</serviceHostingEnvironment>

关于json - 内容类型 'application/json; charset=utf-8' 不是预期的类型 'text/xml; charset=utf-8',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17534010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com