You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XMLHttpRequest发送带$前缀字符串时丢失首个$问题求助

问题分析与解决方案

问题原因

问题出在发送application/x-www-form-urlencoded格式的POST请求时,未对参数值做URL编码。尽管$不属于URL强制编码字符,但ASP.NET的ASMX处理引擎对未编码的开头连续$存在解析异常,导致第一个$被丢失。

解决方案

拼接POST请求体前,必须用encodeURIComponent()对每个参数值编码,确保内容被正确传递到后端。

修改后的JavaScript代码

this.ChangeFilter = function (filter)
{       
    const Url = "NoNameHere.asmx/FunctionThatIsCalled";
    const PostData = "ProfileName={0}&Filter={1}&userKey={2}";

    EventHttpRequest = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
    EventHttpRequest.onreadystatechange = VoidCallback;
    EventHttpRequest.open("post", Url, false);
    EventHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    // 对每个参数值执行URL编码
    EventHttpRequest.send(PostData.replace("{0}", encodeURIComponent(sessionProfile))
                                    .replace("{1}", encodeURIComponent(filter))
                                    .replace("{2}", encodeURIComponent(userKey)));
}

额外修正

你的C#方法参数名存在空格(Profile Name、user Key),这会导致ASP.NET无法正确映射请求参数,请修正为与请求参数名匹配的格式:

public void FunctionThatIsCalled(string ProfileName, string Filter, string userKey)
{}

内容的提问来源于stack exchange,提问作者user988417

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 06:01:07