【发布时间】:2014-01-23 14:34:21
【问题描述】:
我在调用 WCF 服务中的简单方法时遇到问题。我被卡住了,我不知道如何解决这个问题。我将不胜感激。
我的 WCF 服务:
[ServiceContract]
interface IMyService
{
[OperationContract]
string GetSomething();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
class MyService : IMyService
{
[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "GetSomething",
BodyStyle = WebMessageBodyStyle.Bare)]
public string GetSomething()
{
return "Hello";
}
}
启动服务:
using (ServiceHost host = new ServiceHost(typeof(MyService)))
{
host.Open(); // end point specified in app config
Console.WriteLine("Hit Enter to quit.");
Console.ReadLine();
}
App.Config 文件
<configuration>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBinding" crossDomainScriptAccessEnabled="true"/>
</webHttpBinding>
</bindings>
<services>
<service name="testWCF.MyService">
<endpoint address="http://localhost:8003/myservice"
binding="webHttpBinding"
contract="testWCF.IMyService"
behaviorConfiguration="webHttp"/>
</service>
</services>
</system.serviceModel>
</configuration>
这就是 WCF 服务的全部内容。我的 Web 应用程序在 http://127.0.0.1:8085 上运行,这是我从 Web 应用程序发送 jQuery 请求的方式:
$.ajax({
url: "http://127.0.0.1:8003/myservice/GetSomething?callback=?",
dataType: 'jsonp',
cache: false,
beforeSend: function () {
console.log("Loading");
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
},
success: function (data) {
console.log('Success');
console.log(data);
},
complete: function () {
console.log('Finished all tasks');
}
});
我的回复如下: 我可以在我的 chrome 的 JavaScript concole 中看到,该响应已从 WCF 服务发送(GetSomething 方法的内容是“Hello”),但我得到以下控制台输出:
Loading
GetSomething:-1Resource interpreted as Script but transferred with MIME type application/json.
Object
parsererror
Error: jQuery1101030437586596235633_1390485791492 was not called
我的 succes 函数从未执行过。当我关注一些与此类似的帖子时,我不明白它与 Content-Type 有关,但我找不到如何完成这项工作的方法。有人可以帮帮我吗?
【问题讨论】:
标签: jquery json wcf parse-error