使用jquery和参数调用WCF服务 [英] Calling WCF service with jquery and parameters

查看:98
本文介绍了使用jquery和参数调用WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是其中一个基本问题,但我现在用Google搜索和调试了两个小时,错误让我感到厌烦。

Ok, this is one of these basic questions, but I've googled and debugged now for two hours and the error escapes me.

简单场景:带有参数的WCF服务,我想通过jquery调用这些参数。我可以调用没有params的方法,但是使用params,调用永远不会进入我在.NET中的断点。

Simple scenario: WCF service with methods with parameters which I'd like to call through jquery. I can call methods without params alright, but with params, the call never makes it to my breakpoint in .NET.

ServerCode:

ServerCode:

[ServiceContract(Namespace = "http://www.myhost.de")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
    [OperationContract]
    public int TestMeWithParam(int lastId)
    {
        return lastId;
    }

    [OperationContract]
    public int TestMe()
    {
        return 5;
    }
}

Javascript代码

Javascript code

function BaseServiceCall(serviceName, dataInput, successCB, errorCB) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: BaseUrl + "Services/MyService.svc/" + serviceName,
        data: dataInput,
        dataType: "json",
        timeout: 2000,
        success: successCB,
        error: errorCB
    });
}

function ServiceGetMessages(lastMessageId, successCB, errorCB) {
    BaseServiceCall("TestMeWithParam", "{'lastId':'17'}", successCB, errorCB);
    //BaseServiceCall("TestMe", "", successCB, errorCB);
}

因此,如果我调用TestMe服务,则返回5.它有效。 TestMeWithParam永远不会被调用。

So, if I call the TestMe service it returns 5. It works. TestMeWithParam never gets called.

发生了什么?

推荐答案

所以,现在它的工作原理。我不太清楚为什么,因为我没有经常改变。我担心一个因素是我的超时对于调试来说太小了(但即便如此它应该有效)。

So, now it works. I am not very sure why, since I didn't change a lot. I fear one factor was that my timeout was too small for debugging (but even then it should have worked).

所以,现在服务器代码对我有用(有和没有参数)

So, now the server code which works for me (with and without params)

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
    [OperationContract]
    public int TestMeWithParam(int lastId)
    {
        return lastId;
    }

    [OperationContract]
    public int TestMe()
    {
        return 5;
    }
}

正如我在其他地方读到的那样,不需要WebInvoke一点都不只是简单的标准。

As I have read somewhere else, no WebInvoke is needed at all. Just the plain standard.

客户代码:

function BaseServiceCall(serviceName, dataInput, successCB, errorCB) {
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: BaseUrl + "Services/MyService.svc/" + serviceName,
    data: dataInput,
    dataType: "json",
    timeout: 200000,
    success: successCB,
    error: errorCB
});
}

函数ServiceGetMessages(lastMessageId,successCB,errorCB){
BaseServiceCall( TestMeWithParam,{lastId:'+ lastMessageId +'}',successCB,errorCB);
// BaseServiceCall(TestMe,,successCB,errorCB);
}

function ServiceGetMessages(lastMessageId, successCB, errorCB) { BaseServiceCall("TestMeWithParam", '{"lastId":"' + lastMessageId + '"}', successCB, errorCB); //BaseServiceCall("TestMe", '""', successCB, errorCB); }

我改变了Mouhannad建议的引号,尽管我确信之前我曾尝试过。

I changed the quotation marks as suggested by Mouhannad, even though I am sure that I tried that before.

我也尝试过没有lastId但没有用。

I also tried without the "lastId" which did not work.

我之前有过WCF的经历:你玩游戏并玩游戏,然后它可以工作,你不知道为什么。 :(

I had this experience before with WCF: you play around and play around, then it works and you are not sure why. :(

这篇关于使用jquery和参数调用WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆