【问题标题】:jQuery request to WCF service ends with parsererror (Error: jQuery1101030437586596235633_1390485791492 was not called)对 WCF 服务的 jQuery 请求以 parsererror 结尾(错误:未调用 jQuery1101030437586596235633_1390485791492)
【发布时间】: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


    【解决方案1】:

    我自己解决了。问题是,在 App.config 文件中。我添加了bindingConfiguration="webHttpBinding"

    现在我的 App.config 文件是:

    <?xml version="1.0" encoding="utf-8" ?>
    <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"
              bindingConfiguration="webHttpBinding"
              contract="testWCF.IMyService"
              behaviorConfiguration="webHttp"/>
          </service>
        </services>
      </system.serviceModel>
    </configuration>
    

    【讨论】:

      猜你喜欢
      • 2014-03-04
      • 2014-01-11
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-23
      • 2012-04-27
      相关资源
      最近更新 更多