Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is json_encode adding backslashes?
    text
    copied!<p>I've been using <code>json_encode</code> for a long time, and I've not had any problems so far. Now I'm working with a upload script and I try to return some JSON data after file upload.</p> <p>I have the following code:</p> <pre><code>print_r($result); // &lt;-- This is an associative array echo json_encode($result); // &lt;-- this returns valid JSON </code></pre> <p>This gives me the following results:</p> <pre><code>// print_r result Array ( [logo_url] =&gt; http://mysite.com/uploads/gallery/7f/3b/f65ab8165d_logo.jpeg [img_id] =&gt; 54 [feedback] =&gt; Array ( [message] =&gt; File uploaded [success] =&gt; 1 ) ) // Echo result {"logo_url":"http:\/\/mysite.com\/uploads\/gallery\/7f\/3b\/f65ab8165d_logo.jpeg","img_id":"54","feedback":{"message":"File uploaded","success":true}} </code></pre> <p>Can anyone tell me why <code>json_encode</code> adds slashes?</p> <p><strong>update</strong> </p> <p>@Quentin said that something is happening between <code>json_encode</code> and <code>.parseJSON</code> and he's right.</p> <p>Doing a <code>alert(data.toSource());</code> gives me the dollowing result:</p> <pre><code>({response:"{\"logo_url\":\"http:\\/\\/storelocator.com\\/wp-content\\/uploads\\/gallery\\/7f\\/3b\\/71b9520cfc91a90afbdbbfc9d2b2239b_logo.jpeg\",\"img_id\":\"62\",\"feedback\":{\"message\":\"File uploaded\",\"success\":true}}", status:200}) </code></pre> <p>And this is not valid JSON. It also adds the <code>status:200</code> and I have no idea where this comes from.</p> <p>Could it be that the <code>Plupload bind</code> does something to my returned data?</p> <p>This is my js script:</p> <pre><code> uploader.bind('FileUploaded', function(up, file, data) { alert(data.toSource()); $('#' + file.id + " b").html("100%"); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload