从jQuery调用支持AJAX的WCF服务

时间:2022-10-08 09:45:36

I am developing a mobile application using PhoneGap and jQuery Mobile. My aim is to create a web service which will enable the client (mobile) to query against a database.

我正在使用PhoneGap和jQuery Mobile开发移动应用程序。我的目标是创建一个Web服务,使客户端(移动)能够查询数据库。

After some research, I found out that AJAX Enabled Services might be what I was looking for. So, I began by creating an AJAX-Enabled WCF Service and for now I added only the following method:

经过一番研究,我发现AJAX Enabled Services可能就是我想要的。所以,我首先创建了一个支持AJAX的WCF服务,现在我只添加了以下方法:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]
public string GetString()
{
    return "Hello there";
}

My web.config looks like this:

我的web.config看起来像这样:

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>

    <system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="WebApplication1.MobileServiceAspNetAjaxBehavior">
                    <enableWebScript />
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
            multipleSiteBindingsEnabled="true" />
        <services>
            <service name="WebApplication1.MobileService">
                <endpoint address="" behaviorConfiguration="WebApplication1.MobileServiceAspNetAjaxBehavior"
                    binding="webHttpBinding" contract="WebApplication1.MobileService" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

After finishing off this service, I called from the client using the following method:

完成此服务后,我使用以下方法从客户端调用:

$.ajax({
    type: "POST",
    url: "http://localhost:11634/MobileService.svc/GetString",
    contentType: "application/json",
    data: "{}",
    dataType: "json",
    success: function (result) {
    $("#textbox").text(result);
    },        
    error: function (textStatus) {
        alert(textStatus);
    }
});

When calling the service, I am getting the following error [object Object]. Can you guide me on what am I doing wrong and whether I am using the right technologies please?

调用服务时,我收到以下错误[object Object]。你能指导一下我做错了什么以及我是否正在使用合适的技术吗?

3 个解决方案

#1


1  

As Tariqulazam rightly points out [Object object] is not an error but a response object. To access the data you could modify your code to read:

正如Tariqulazam正确地指出[Object object]不是错误而是响应对象。要访问数据,您可以修改代码以阅读:

success: function (result) {     
    var data = result.d
    $("#textbox").text(data);     
},

If you want to see a text-book example the following looks like a good example of jQuery code which consumes a WCF web-service:

如果您想查看教科书示例,以下内容看起来就像是一个使用WCF Web服务的jQuery代码的好例子:

Consuming WCF service using jQuery

使用jQuery使用WCF服务

Hope this helps!

希望这可以帮助!

#2


0  

You need to wrap your $.ajax({ ... }) inside a function call. That way on a specific action it is called.

您需要将$ .ajax({...})包装在函数调用中。这就是所谓的特定动作。

#3


0  

Since you are not passing parameters, I would change this to a GET call instead and remove the data portion of the ajax call. Also the way you were returning it was in XML format, change the response format to JSON

由于您没有传递参数,我会将其更改为GET调用,并删除ajax调用的数据部分。您返回的方式也是XML格式,将响应格式更改为JSON

    [OperationContract]
    [WebGet(UriTemplate = "/GetString", RequestFormat = WebMessageFormat.Json)]
    public string GetString()
    {
        return "Hello there";
    }

$.ajax({     
    type: "GET",     
    url: "http://localhost:11634/MobileService.svc/GetString",     
    contentType: "application/json",     
    dataType: "json",     
    success: function (result) {     
    $("#textbox").text(result);     
    },             
    error: function (textStatus) {     
        alert(textStatus);     
    }     
});

#1


1  

As Tariqulazam rightly points out [Object object] is not an error but a response object. To access the data you could modify your code to read:

正如Tariqulazam正确地指出[Object object]不是错误而是响应对象。要访问数据,您可以修改代码以阅读:

success: function (result) {     
    var data = result.d
    $("#textbox").text(data);     
},

If you want to see a text-book example the following looks like a good example of jQuery code which consumes a WCF web-service:

如果您想查看教科书示例,以下内容看起来就像是一个使用WCF Web服务的jQuery代码的好例子:

Consuming WCF service using jQuery

使用jQuery使用WCF服务

Hope this helps!

希望这可以帮助!

#2


0  

You need to wrap your $.ajax({ ... }) inside a function call. That way on a specific action it is called.

您需要将$ .ajax({...})包装在函数调用中。这就是所谓的特定动作。

#3


0  

Since you are not passing parameters, I would change this to a GET call instead and remove the data portion of the ajax call. Also the way you were returning it was in XML format, change the response format to JSON

由于您没有传递参数,我会将其更改为GET调用,并删除ajax调用的数据部分。您返回的方式也是XML格式,将响应格式更改为JSON

    [OperationContract]
    [WebGet(UriTemplate = "/GetString", RequestFormat = WebMessageFormat.Json)]
    public string GetString()
    {
        return "Hello there";
    }

$.ajax({     
    type: "GET",     
    url: "http://localhost:11634/MobileService.svc/GetString",     
    contentType: "application/json",     
    dataType: "json",     
    success: function (result) {     
    $("#textbox").text(result);     
    },             
    error: function (textStatus) {     
        alert(textStatus);     
    }     
});