Javascript 对 ASP.NET Web 服务 (ASMX) 的 JSON 请求中区分大小写何时重要?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2738321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:43:24  来源:igfitidea点击:

When is case sensitivity important in JSON requests to ASP.NET web services (ASMX)?

asp.netjavascriptajaxweb-servicesjson

提问by Ben McCormack

I've done the following tests with JSON requests sent to an ASP.NET 2.0 ASMX web service (using AJAX Extensions 1.0 for ASP.NET 2.0) and it seems that case sensitivity is important in some situations but not in others. See the following examples:

我已经对发送到 ASP.NET 2.0 ASMX Web 服务的 JSON 请求(使用 AJAX Extensions 1.0 for ASP.NET 2.0)进行了以下测试,似乎区分大小写在某些情况下很重要,但在其他情况下则不重要。请参阅以下示例:

  • Case matches 100%:

    {"request":{"Address":{"Address1":"123 Main Street","Address2":"suite 20","City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}}
    

    Result: HTTP/1.1 200 OK

  • Case of contained object name Addressdoes not match:

    {"request":{"address":{"Address1":"123 Main Street","Address2":"suite 20","City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}}
    

    Result: HTTP/1.1 200 OK

  • Case of web service parameter requestdoes not match:

    {"Request":{"address":{"Address1":"123 Main Street","Address2":"suite 20","City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}}
    

    Result: HTTP/1.1 500 Internal Server Error

  • 大小写匹配 100%:

    {"request":{"Address":{"Address1":"123 Main Street","Address2":"suite 20","City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}}
    

    结果: HTTP/1.1 200 OK

  • 包含对象名称的大小写Address不匹配:

    {"request":{"address":{"Address1":"123 Main Street","Address2":"suite 20","City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}}
    

    结果: HTTP/1.1 200 OK

  • web服务参数request不匹配的情况:

    {"Request":{"address":{"Address1":"123 Main Street","Address2":"suite 20","City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}}
    

    结果: HTTP/1.1 500 Internal Server Error

(Quick note: The fact that the class Requestand the parameter requestshare the same name is not relavant. Even if I change the parameter name to lrequest, case sensitivity is still required.)

(快速说明:类Request和参数request共享同名的事实并不相关。即使我将参数名称更改为lrequest,仍然需要区分大小写。)

When is case sensitivity in JSON Web Service requests important?Also, is this a general web service issue or is this specific to ASP.NET AJAX?

JSON Web 服务请求中的区分大小写何时重要?另外,这是一个一般的 Web 服务问题还是特定于 ASP.NET AJAX?



Additional background information:

其他背景信息:

I'm using the AJAX Extensions 1.0 for ASP.NET 2.0, so this may have been addressed in later versions of the framework. If so please let me know.

我正在为 ASP.NET 2.0 使用 AJAX 扩展 1.0,所以这可能在框架的更高版本中得到解决。如果是这样,请告诉我。

After following up to the answers in my most recent question regarding formatting JSON strings, I realized that the reason my request was failing wasn't because of invalid JSON (thanks to T.J. Crowderfor pointing that out and linking to http://www.jsonlint.com/for JSON validation). Rather, after doing some more testing, I learned that the problem was because the web service didn't how my JSON object was formatted and I discovered the web service was very pickyin regards to case sensitivity. It seems that sometimes case sensitivity is important, whereas other times it is not (see examples above).

在跟进我最近关于格式化 JSON 字符串的问题中的答案后,我意识到我的请求失败的原因不是无效的 JSON(感谢TJ Crowder指出并链接到http://www. jsonlint.com/用于 JSON 验证)。相反,在进行了更多测试后,我了解到问题是因为 Web 服务没有格式化我的 JSON 对象,而且我发现 Web 服务在区分大小写方面非常挑剔。似乎有时区分大小写很重要,而有时则不重要(请参见上面的示例)。

Here's what my C# code for the web method and classes looks like:

这是我的 Web 方法和类的 C# 代码的样子:

[WebMethod]
public Response ValidateAddress(Request request)
{
    return new test_AddressValidation().GenerateResponse(
        test_AddressValidation.ResponseType.Ambiguous);
}

...

public class Request
{
    public Address Address;
}

public class Address
{
    public string Address1;
    public string Address2;
    public string City;
    public string State;
    public string Zip;
    public AddressClassification AddressClassification;
}

public class AddressClassification
{
    public int Code;
    public string Description;
}

回答by Serapth

According to JSON-RPC spec, the answer is always.

根据JSON-RPC 规范,答案始终是。

9.0 Case-Sensitivity of Procedure and Parameter Names

Conforming implementations MUST treat procedure and parameter names as being case-sensitive such the names bar and BAR would be seen as two distinct entities.

9.0 过程和参数名称区分大小写

符合标准的实现必须将过程和参数名称视为区分大小写,例如名称 bar 和 BAR 将被视为两个不同的实体。

So, it sounds like the situations when it worked for you were the exceptions, not the cases where they didn't. Chances are someone on some side of the equation was just not adhering to the specs.

所以,听起来它对你有用的情况是例外,而不是它们没有的情况。很可能有人在等式的某个方面只是不遵守规范。