tags:

views:

116

answers:

4

I am using the jQuery ajax method to get data from the database and return the data via a json object. However one of the values in this json object is an HTML string. I am basically doing exactly what is seen here except I need to know how I can remove the slashes that jQuery is adding to the HTML string. Any ideas?

Example

json.HTML = Click <a href=\"http://example.com/example.php\"&gt;here&lt;/a&gt;;

//Needs to be
json.HTML = Click <a href="http://example.com/example.php"&gt;here&lt;/a&gt;;

I was hoping to do this without code if possible.

UPDATE

Ok I found that if I do htmlentites before it is returned, then the slashes are not there when the value comes in. Now, which jquery function would I use to insert this string inside a td element without slashes being added by .html or .text functions.

Here is what it looks like directly from the json.HTML value,

Click &lt;a href=\&quot;http://example.com\&amp;quot;&amp;gt;here&amp;lt;/a&amp;gt;

And here is after it is displayed using .html

Click <a href=\"http://example.com\"&gt;here&lt;/a&gt;

And here is after is it displayed using .text

Click &lt;a href=\&quot;http://example.com\&amp;quot;&amp;gt;here&amp;lt;/a&amp;gt;

I wonder if I need to use .val maybe? Oh one more thing, I want this HTML to be display literally, not the actual HTML to be inserted into the code.

+1  A: 

Could it be as simple as:

stringvar.replace('\\','');
Fosco
I was hoping there was an option I could change in jQuery to have it done automatically. I would like to do it without code if possible.
Metropolis
A: 

Hello U just check the link bellow It may Help U.

http://stackoverflow.com/questions/3787309/consume-soap-webservice-using-jquery/3801254#3801254
Umakanta.Swain
+2  A: 

I don't think your problem is jquery adding slashes. I don't think your returning the json properly form your php are you using json_encode and giving json header ? what does the json result look like if you save it as a text file ?

also if you can't get it to come down properly you can use

unescape function

out put this as your json

    $foo =  'Click &lt;a href=&quot;http://example.com&amp;quot;&amp;gt;here&amp;lt;/a&amp;gt;';

    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
    header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
    header("Cache-Control: no-cache, must-revalidate" ); 
    header("Pragma: no-cache" );
    header("Content-type: text/x-json");
    echo json_encode($foo);

and see if does the same thing

EDIT dont use text you want something like

       $('#table_id td').html(json_response);

that is pseudo code

mcgrailm
Yes, I am using json_encode and giving json header
Metropolis
I added some more code above
Metropolis
Using the above and .text to ouput it is displaying this - Click <a href="http://example.com">here</a>
Metropolis
ok so you see there i no slash being added by jquery now see my new edit to come
mcgrailm
Ok awesome. I that may be it....I am going to test it for a few more minutes and get back to you.
Metropolis
Nope It is still outputting slashes. I thought it was working but it is not.
Metropolis
so the code i gave you outputs slashes ? can I see the php code that your using ?
mcgrailm
I figured out the problem. I needed to strip the slashes on the POST data, which I was doing in one spot, but I was then using the POST data without stripping them in another spot. Sorry for all of the hassle for that. I really appreciate your help.
Metropolis
No problem at at all. Glad I could help and now you know that jQuery does not add slashes too. As a side note I would discourage the use of the word "CLICK" before your link your link should be enough for the user to know to click it not only that in your context the user may try to click the word "CLICK" just my two cents
mcgrailm
@mcgrailm: LOL I know, I was just using that as an example, sorry about that. I do not actually use the work click in normal context.
Metropolis
LOL good to know
mcgrailm
would you please accept my answer ?
mcgrailm
A: 

After playing around with this for a VERY long time. I found out that I was stripping the slashes from the post data and storing that value in an array which was correct, but I was then later using the post values raw again which is where the slashes were coming from. All I needed to do was use the value that I had already stripped the slashes on.

Metropolis