jquery - 使用jquery和参数调用WCF服务

标签 jquery ajax wcf

好吧,这是这些基本问题之一,但我已经用谷歌搜索并调试了两个小时,但我没有发现错误。

简单场景:WCF 服务,其方法带有我想通过 jquery 调用的参数。我可以调用不带参数的方法,但是使用参数时,调用永远不会到达 .NET 中的断点。

服务器代码:

[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代码

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 永远不会被调用。

发生什么事了?

最佳答案

当我尝试将 jquery 与 wcf 一起使用时,我遇到了类似的问题! 尝试改变这一点:

"{'lastId':'17'}" to '{"lastId":"17"}'
//i know you tried something similar but try putting the brackets around the number as well

如果不起作用,请告诉我,我将编辑我的帖子以与您一起解决解决方案。

附:你也应该尝试@Bryce的建议!

[WebInvoke(Method = "POST",
           BodyStyle = WebMessageBodyStyle.Wrapped,
           ResponseFormat = WebMessageFormat.Json,
           RequestFormat = WebMessageFormat.Json)]
//note the response and request format

关于jquery - 使用jquery和参数调用WCF服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3876784/

相关文章:

.net - WCF 返回数组而不是列表即使集合类型 == Generic.List

asp.net - 窗口服务

wcf - 如何为单个方案配置多个 WCF 绑定(bind)配置

javascript - 滚动jquery更改html标题标签背景图像

javascript - 将 JSON 解析为 HTML 嵌套列表

javascript - 为什么在这里看到 “origin is not allowed by Access-Control-Allow-Origin”错误? [复制]

javascript - 无需提交表单即可获取输入字段的值

javascript - 如何缩放元素以适合屏幕

jquery - 让光滑的图像 slider 始终覆盖为背景

javascript - wp_enqueue_script 或 wp_head