jQuery AJAX 调用带有特殊字符的未定义错误

发布于 2024-08-05 11:45:52 字数 588 浏览 2 评论 0原文

我尝试使用 jQuery 进行 AJAX 调用,数据有特殊字符,例如 {'data':'

test

'}。看来一开始就没有传递这个数据。如果我只是通过 {'data':'test'} 就会起作用。由于特殊字符 <,encodeURIComponent 和 JSON.stringify 在此失败。 > /。

有人可以帮忙吗?谢谢。

$.ajax({
    type: "POST",
    url: "services.aspx",
    data: "data=" + encodeURIComponent(JSON.stringify(obj)),
    dataType: "text",
    error: function(xhr, textStatus, errorThrown)   {   
        alert("ERROR"); },
    success: function(data)
            {   

            }               
}); 

问候,

大卫

I tried to make an AJAX call using jQuery, the data has special characters, e.g {'data':'<p>test</p>'}. It seems failed to pass this data in the first place. It will work if i just pass {'data':'test'}. encodeURIComponent and JSON.stringify failed here due to the special character < > /.

Could anyone please help with it? Thanks.

$.ajax({
    type: "POST",
    url: "services.aspx",
    data: "data=" + encodeURIComponent(JSON.stringify(obj)),
    dataType: "text",
    error: function(xhr, textStatus, errorThrown)   {   
        alert("ERROR"); },
    success: function(data)
            {   

            }               
}); 

Regards,

David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静谧幽蓝 2024-08-12 11:45:52

我在 firebug 中对此进行了快速测试,它实际上工作得很好,数据已发送以及所有内容,所以听起来您的问题与 ajax 调用本身无关,而是与您发布到的函数有关。

I gave this a quick test in firebug, and it actually worked just fine, data was sent and everything, so it sounds like your problem is not related to the ajax call itself but the function you are posting to.

素手挽清风 2024-08-12 11:45:52

这种类型的问题有时很难调试,因为有太多组件接触您的数据,并且每个组件都需要自己的引用或转义风格,以确保您的数据按您的预期通过。

首先要做的是确保数据正确到达 ajax 函数。在 ajax 函数之前,使用 console.logalert() 查看数据。根据数据的来源,此时的数据甚至可能不正确。

您可以使用 Firebug 的 Net 面板来查看实际向服务器发出的请求,以查看离开浏览器的数据。如果您有权访问服务器,则可以在 ajax 函数处理程序中进行调试,以查看它接收到的数据。

基本上,您必须从数据开始的地方走完整条路线,到数据错误的地方,并找到错误转弯的点。

This type of problem is sometimes tricky to debug, because so many components touch your data, and each needs its own style of quoting or escaping to be sure your data gets through as you intended.

The first thing to do is to make sure the data is getting to the ajax function properly. Just before the ajax function, use a console.log or alert() to see what the data looks like. Depending on where the data is coming from, it may not even be correct at that point.

You can use Firebug's Net panel to look at what request was actually made to the server to see the data leaving the browser. If you have access to the server, you may be able to debug within the ajax function handler there to see what data it received.

Basically, you have to walk the entire trail from where the data begins, to where the data is wrong, and find the point at which it made a wrong turn.

时间海 2024-08-12 11:45:52

假设 encodeURIComponent(JSON.stringify(obj)) 中的 obj 是字符串或 json 对象,那么您的脚本应该可以工作。

如果 obj = {'data':'

test

'}; 那么您不需要 encodeURIComponent 您只需执行 data: JSON.stringify(obj)

代码还有更多内容吗,如果您可以发布它,可能会有更多帮助。

Assuming that obj in encodeURIComponent(JSON.stringify(obj)) is a string or a json object then your script should work.

If obj = {'data':'<p>test</p>'}; then you don't need the encodeURIComponent you could just do data: JSON.stringify(obj)

Is there any more to the code, it might help more if you could post it.

失去的东西太少 2024-08-12 11:45:52

我不是 asp 开发人员,但在通过 jquery ajax 处理 html post 时遇到了同样的问题,我曾经这样发帖:

var data = 'id='+ escape(currid) +'&html='+ escape(div_html);

$.post("http://...", data, ...);

希望这能更好地帮助您。

I'm not an asp dev but I got the same issue while handling html post via jquery ajax I used to post like this:

var data = 'id='+ escape(currid) +'&html='+ escape(div_html);

$.post("http://...", data, ...);

Hope this will help you better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文