Javascript 使用Ext.js从WCF获取数据;网络错误400错误请求“;

Javascript 使用Ext.js从WCF获取数据;网络错误400错误请求“;,javascript,ajax,web-services,wcf,extjs,Javascript,Ajax,Web Services,Wcf,Extjs,我正在尝试使用Ext.js从WCF获取数据。我想点击一个按钮,获取所有数据。以下是我迄今为止所做的尝试: 这是我的JavaScript函数: buttonClick: function () { var myparams = { 'name': 'baris' }; Ext.Ajax.request({ url: 'http://localhost:57044/www/HelloWorldService.svc/GetMessage',/

我正在尝试使用Ext.js从WCF获取数据。我想点击一个按钮,获取所有数据。以下是我迄今为止所做的尝试: 这是我的JavaScript函数:

buttonClick: function () {
        var myparams = { 'name': 'baris' };

        Ext.Ajax.request({
            url: 'http://localhost:57044/www/HelloWorldService.svc/GetMessage',//replace with ajax call
            //params: Ext.encode(myparams),
            method: 'GET',
            type: 'ajax',
            success: function (response, opts) {
                if (response.responseText) {
                    Ext.MessageBox.alert('Success', response.responseText);
                }
                else {
                    Ext.MessageBox.alert('Failed', 'Unable to get');
                }
            },
            failure: function (response, opts) {
                alert("failure="+response.responseText);
            }
        });
    }
这是我的web服务接口代码:

namespace Mobile.Service.Maintenance
{
    [ServiceContract]
    interface IHelloWorldService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped,
               RequestFormat = WebMessageFormat.Json,
               ResponseFormat = WebMessageFormat.Json)]
        String GetMessage();
    }
}
这是web服务类代码:

namespace Mobile.Service.Maintenance
{
    public class HelloWorldService : IHelloWorldService
    {

        public String GetMessage()
        {
            return "Hello world from " +  "baris" + "!";
        }
    }
}
我使用教程实现了WCF服务。我创建了一个控制台应用程序并使用了WCF,但是使用Ext.js和Ajax做同样的事情失败了。如果我单击该按钮,它总是返回失败函数。在失败函数response.responseText中,输出一个空字符串

我在Firebug中遇到以下错误: 网络错误:400错误请求-http://localhost:57044/www/HelloWorldService.svc/GetMessage?\u dc=1399642562150

(Yanıt Başlıkları=响应标题,ıstem Başlıkları=请求标题抱歉土耳其本地化) 提前谢谢

这是萤火虫的截图。

让我们了解WCF服务提供的服务的概念。WCF服务提供了两种服务

1) 基于SOAP的服务。

2) 基于Rest的服务-现在已被Web API取代(但仍受WCF支持,如果您想开发基于Rest的服务,请改用Web API)

第一次学习本教程时,您构建了基于SOAP的服务,并且能够从控制台应用程序中使用该服务,因为基于.net的控制台应用程序具有支持基于SOAP的服务的必要工具

第二次尝试使用服务时,您尝试使用基于rest的服务,因为Javascript(本例中为ExtJs)不支持使用基于SOAP的服务。至少我还没有遇到任何javascript框架能够充分利用浏览器中基于SOAP的服务

所以,如果您想从web浏览器(本例中为ExtJS)使用这些服务,您应该开发基于REST的服务

要实现这一目标,代码中几乎没有什么需要更改的地方

namespace Mobile.Service.Maintenance
{
    [ServiceContract]
    interface IHelloWorldService
    {
        [OperationContract]
        [WebGet(BodyStyle = WebMessageBodyStyle.Wrapped,
               RequestFormat = WebMessageFormat.Json,
               ResponseFormat = WebMessageFormat.Json
               )]
        String GetMessage();
    }
}
更改您的web.config,以便您尝试提供WCF Restful服务

在web.config的行为部分下添加以下端点行为

 <endpointBehaviors>
        <behavior name="restfulbehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>

添加以下端点行为

<services>
      <service name="Mobile.Service.Maintenance.HelloWorldService">
        <endpoint address="" behaviorConfiguration="restfulbehavior"
                  binding="webHttpBinding"
                  bindingConfiguration="" 
contract="Mobile.Service.Maintenance.IHelloWorldService"/>


      </service>
    </services>

首先尝试使用浏览器运行服务

localhost:57044/www/HelloWorldService.svc/GetMessage

如果它的浏览器显示来自酒吧的hello world,恭喜您拥有了第一个基于restful web的服务

stackoverflow不允许我发布图像,请在

在这里可以找到一篇关于restful服务的优秀文章

希望这有帮助

谢谢你

谢谢你的回答太好了。顺便说一句,欢迎来到so。