简体   繁体   English

Ajax 调用未命中 C# 代码后面的 web 方法

[英]Ajax call not hitting the webmethod in C# Code Behind

Good morning.早上好。 Currently I'm having a problem having my ajax call go from the JQuery to C# code behind.目前,我的 ajax 调用 go 从 JQuery 到 ZD7EFA19FBE243972FDB1A75A89A6BZ 代码在后面时遇到问题。 After that's successful it should return the true value at the end of the function.成功后,它应该在 function 的末尾返回真值。 I've tried removing the type and changing the web method to an HttpPost header which I can't do.我尝试删除类型并将 web 方法更改为 HttpPost header ,但我无法做到。 Here's an example of the code.这是代码的示例。

Javascript Javascript

var jsonFormValues = JSON.stringify(formValues);
    $.ajax({
        type:POST,
        async: false,
        url: "RegistryOpt.aspx/SendOpt",
        data: { jsonFormValues: jsonFormValues },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            onWebMethodSucceeded();
        }
    });

C# C#

[WebMethod(EnableSession = true)]
public static bool SendOpt(string jsonFormValues)
{
    Debug.Assert(!string.IsNullOrEmpty(jsonFormValues));

    var fv = Json.Decode<FormValues>(jsonFormValues);

    fv.attestationItems = RegistryOpt.FormatAttestation(fv.attestation);

    var eb = new EmailBuilder(fv);

    return true;
}

I think the problem is that you are formatting to JSON only parameter value.我认为问题在于您仅格式化为 JSON 参数值。 Instead of var jsonFormValues = JSON.stringify(formValues);而不是var jsonFormValues = JSON.stringify(formValues); , you need to use JSON.stringify for parameter itself too: ,您也需要将JSON.stringify用于参数本身:

    $.ajax({
        type:POST,
        async: false,
        url: "RegistryOpt.aspx/SendOpt",
        data: JSON.stringify({ jsonFormValues: formValues }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            onWebMethodSucceeded();
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM