JQuery AJAX call undefined error with special characters

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

Can anyone help with this? 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) { } }); 

Hello,

David

+4
source share
4 answers

I gave this quick test in firebug, and it really worked just fine, the data was sent and that’s it, so it seems that your problem is not with the ajax call itself, but with the function you are sending.

+3
source

This type of problem is sometimes difficult to debug, because so many components relate to your data, and each of them needs its own citation or escaping style to make sure that your data passes as you wish.

The first thing to do is make sure that the data gets into the ajax function correctly. Before the ajax function, use console.log or alert() to see how the data looks. Depending on where the data is coming from, it may not even be right at this point.

You can use the Firebug Net panel to see what request was actually made to the server to see the data coming out of the browser. If you have access to the server, you can debug inside the ajax function handler to find out what data it received.

Basically, you need to go all the way where the data starts from, where the data is incorrect, and find the point at which it made the wrong turn.

+1
source

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

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

Is there more code, it can help if you can post it.

-one
source

I am not asp dev, but I have the same problem when processing an html message through jquery ajax, which I used to publish as follows:

 var data = 'id='+ escape(currid) +'&html='+ escape(div_html); $.post("http://...", data, ...); 

Hope this helps you better.

-2
source

All Articles