Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange $.post() AJAX error when parsing JSON
    primarykey
    data
    text
    <p>I am facing this strange error in using <code>$.post</code>.</p> <p><strong>works</strong></p> <pre><code>$("#add-video").click(function(){ var url = $("#new-video-url").val(); $('#loader').show(); $.post( base_url + "forms/coach/get_url.php", { url:url, base_url:base_url }, function(data){ alert(data); $('#loader').hide(); }); }); </code></pre> <p>The above piece of code, shows me the json array I am receiving using a php file, and also shows the <code>title</code> field here, and hides the loader image.</p> <p>But when I <code>alert(data.title)</code>, it shows me undefined. More over, when I add datatype 'json' to <code>$.post</code>,</p> <p><strong>doesn't work</strong></p> <pre><code>$("#add-video").click(function(){ var url = $("#new-video-url").val(); $('#loader').show(); $.post( base_url + "forms/coach/get_url.php", { url:url, base_url:base_url }, function(data){ alert(data); $('#loader').hide(); }, "json"); //Added datatype here. }); </code></pre> <p>This neither alerts anything nor does it hide the loader image. I also tried,</p> <pre><code>$("#add-video").click(function(){ var url = $("#new-video-url").val(); $('#loader').show(); $.post( base_url + "forms/coach/get_url.php", { url:url, base_url:base_url }, function(data){ jQuery.parseJSON(data); alert(data.title); $('#loader').hide(); }); }); </code></pre> <p>The above one too neither alerts anything nor does it hide the loader. And then I tried this one too that did nothing.</p> <pre><code>$("#add-video").click(function(){ var url = $("#new-video-url").val(); $('#loader').show(); $.post( base_url + "forms/coach/get_url.php", { url:url, base_url:base_url }, function(data){ jQuery.parseJSON(data); //tried without this too. alert(data['title']); $('#loader').hide(); }); }); </code></pre> <p>The strangest thing is that I have previously used json as I have shown in the 2nd script(out of 4), and that works normally. My JS console too doesn't show any errors or warning. What am I doing wrong here? How do I access the title field of data?</p> <p>If this helps, here is how I send the json array,</p> <pre><code>$json = array("title" =&gt; $title, "embed" =&gt; $embed, "desc" =&gt; $desc, "duration" =&gt; $duration, "date" =&gt; $date); print_r(json_encode($json)); </code></pre> <p>I would really appreciate if someone can point out the error and tell me why my scripts are failing, similar functions worked in other js file.</p> <p>here is my data, that is returned by server,</p> <blockquote> <p>{"title":"Sunn Raha Hai Na Tu Aashiqui 2 Full Song With Lyrics | Aditya Roy Kapur, Shraddha Kapoor","embed":"\r\t\t\t\t\t\r\t\t\t\t\t&lt;/param>&lt;/param>\r\t\t\t\t\t&lt;/param>\r\t\t\t\t\t\r\t\t\t\t\t&lt;/embed>&lt;/object>","desc":"Presenting full song \"Sun Raha Hai Na Tu\" with lyrics from movie \"Aashiqui 2\" produced by T-Series Films &amp; Vishesh Films, starring Aditya Roy Kapur, Shraddha Kapoor in voice of Ankit Tiwari. \n\nSong: SUNN RAHA HAI\nSinger: ANKIT TIWARI\nMusic Director: ANKIT TIWARI\nAssistant Mix Engineer - MICHAEL EDWIN PILLAI\nMixed and Mastered by ERIC PILLAI (FUTURE SOUND OF BOMBAY)\nLyrics:SANDEEP NATH\nMovie: AASHIQUI 2\nProducer: BHUSHAN KUMAR KRISHAN KUAMR Producer: MUKESH BHATT<br> \nDirector: MOHIT SURI\nMusic Label: T-SERIES\n\nBuy from iTunes - https://itunes.apple.com/in/album/aashiqui-2-original-motion/id630590910?ls=1\n\nEnjoy &amp; stay connected with us!! \n\nSUBSCRIBE T-Series channel for unlimited entertainment\nhttp://www.youtube.com/tseries\n\nCircle us on G+ \nhttp://www.google.com/+tseriesmusic\n\nLike us on Facebook\nhttp://www.facebook.com/tseriesmusic\n\nFollow us\nhttp://www.twitter.com/_Tseries","duration":"391","date":"2013-04-03"}</p> </blockquote> <p><strong>Edit</strong></p> <p>This worked suddenly.. :o</p> <pre><code> $("#add-video").click(function(){ var url = $("#new-video-url").val(); $('#loader').show(); $.post( base_url + "forms/coach/get_url.php", { url:url, base_url:base_url }, function(data){ alert(data.desc); console.log(data.desc); $("#loader").hide(); }, "json"); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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