0

I'm sending some data from php through ajax to jquery.

If $content="ABC"; everything is OK. I get alert with ABC.

If $content="<div>ABC</div>"; then Houston has a problem. Nothing happens at all.

Here is PHP code

$json = json_encode(array("content" => $content));
echo $json;

And this is Jquery

 $('#'+pic_type+'_form_n_'+pic_number).ajaxSubmit({
 success: function(responseimage){
 result = jQuery.parseJSON(responseimage);
 alert(result.content);

Any ideas ?

UPDATE! I've removed jQuery.parseJSON so that line has only this code

result = responseimage;

And now I get the result in alert. The result is the following

{"content":".<div>ABC&lt;\/div&gt;."}</div>

So we can see that JSON is not created well. I;ve tried utf8_encode and trim , but they do nothing to the result. result is strange.

19
  • 1
    Wrapping html in json is [almost always] plain wrong. Send raw data, and style it via client-side templating. Commented Dec 6, 2012 at 11:39
  • I'm sending multiple data from PHP. That's why I need JSON, I've wrote only part of the code that's why you see only one JSON object Commented Dec 6, 2012 at 11:41
  • Yeah, I am just advising you to send raw data [text, strings, numerals], then to create DOM stuff in the browser. In case you are dealing with meta HTML [e.g. snippets of code like in Gist], it makes sense to send html code, but properly escaped as well. Commented Dec 6, 2012 at 11:46
  • What is the json you get in your javascript ? Commented Dec 6, 2012 at 11:51
  • @moonwave99 I understand you, but building html elements with javascript will be pain in .... for me :) maybe there is another way to do it the way I wanted? Commented Dec 6, 2012 at 11:52

3 Answers 3

0

PHP (and JSON) code seems fine, provided that the variable $content actually has any content. Else the PHP script will fail and there's your problem.

Did you define the dataType as "json" in the AJAX request?

$.ajax({
    url: 'json.php',
    dataType: 'json',
    success:function(data){
        console.log(data);
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

I do not use $.ajax, instead of that I'm sending the whole form using jquery_form plugin. I've updated th first post so you could see the code
0

I think you just need to utf8_encode your response before doing json_encode. Just change this :

$content = utf8_encode("<div>ABC</div>");

5 Comments

no luck. Again error > SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
Can u try die( $json ) instead of echo $json in your php code ?
I've tried but no luck. The same error > SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
Also I'm googling this error and here is what I've found. stackoverflow.com/questions/7972110/… by the I use dreaviewer but The answer in that link is a bit complicated for me.
did you try trimming the $json using trim($json) before doing echo ?
0

You are delivering invalid json somehow - please put your raw response in JSONLint, it will tell you what is wrong.

6 Comments

Parse error on line 1: <div>ABC</div> ^ Expecting '{', '[' Now what does that mean ?????
By raw response I intend the echo of json_encode(array("content" => $content));, not just the html of course.
I have no info about what will come from that echo cause script fails to perform on my server. So what I can put there ? I have no result to test
I thout it kinda php tester and put there php lines but it does not works
Frankly speaking, I really advise you to find a nice resource about how web works, and learn it from scratch: just patching a bad designed script with no clue about formats and standards won't help you at all.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.