json_encode not working with a html string as value

前端 未结 8 1637
太阳男子
太阳男子 2020-12-03 10:02

I am debugging this ajax for quite a time now. I have this on my jQUery file:

$(\"#typeForm\").ajaxForm({
    success : function(html){
        alert(html);
         


        
相关标签:
8条回答
  • 2020-12-03 10:19
    string(214) "{"file_name":"cde595988d386529909ce5a8fe3a6d6f.png","prompt":"<div style="position:relative;"><img src="\/assets\/ui\/success.png" \=""><span style="position:relative;top:-15px;">Nachricht empfangen!&lt;\/span&gt;&lt;\/div&gt;"}"
    </span></div>
    

    This seems to be broken because there's no quote escaping. When an unescaped " is found, it breaks the JSON structure you expect. Escaped " should be \", single quotes with \' and so on.

    0 讨论(0)
  • 2020-12-03 10:21

    How about convert all the potential problem characters instead of just what fixes the problem in this circumstance:

    die(json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE));
    

    http://php.net/manual/en/function.json-encode.php

    http://php.net/manual/en/json.constants.php

    0 讨论(0)
  • 2020-12-03 10:21

    Some stuff to try:

    ajaxForm supports dataType argument, if you expect a JSON coming from the server, use dataType: json like so

    $("#typeForm").ajaxForm({
        success : function(html){
           // html here is already automatically a json object
           alert(html.prompt);
        },
        dataType: 'json'
    }).submit();
    

    Could you post the full service.php? OR try the following:

    exit(json_encode(array('file_name' => $data['upload_data']['file_name'], 'prompt' => $str)));

    -- EDIT --

    Not sure why json_encode returns such weird string :s, is the json_encode a standard php library or an external library? I'm asking this because some servers don't have json_encode in their php installation... I tested on my local and using php internal json_encode and it works fine:

    <?php
    $str = "<div style='position:relative'><img src='/assets/ui/success.png' /><span style='position:relative;top:-15px;'>Nachricht empfangen!</span></div>";
    echo json_encode(array('prompt' => $str));
    
    // output
    //{"prompt":"<div style='position:relative'><img src='\/assets\/ui\/success.png' \/><span style='position:relative;top:-15px;'>Nachricht empfangen!<\/span><\/div>"}
    
    0 讨论(0)
  • 2020-12-03 10:28

    If you cannot find a better solution for this you can encode the value to base64 encoding:

    $data = array('upload_data' => $this->upload->data());
    $str = base64_encode("<div style='position:relative'><img src='/assets/ui/success.png' /><span style='position:relative;top:-15px;'>Nachricht empfangen!</span></div>");
    echo json_encode(array('file_name' => $data['upload_data']['file_name'], 'prompt' => $str));
    

    and in the client decode it, IMO this is more safer this is also more applicable if you're processing characters from different languages.

    ALSO:

    to sure that no other characters will be added on the json string call exit; writer after you print it.

    0 讨论(0)
  • 2020-12-03 10:28

    It looks like you need to escape your quotes server-side. Since they are in there, it seems to be creating an invalid JSON string.

    0 讨论(0)
  • 2020-12-03 10:33

    I was having same problem with json_encode today. But after testing a lot I found the correct solution:

    In PHP to encode the array or string:

    json_encode($array, JSON_HEX_QUOT | JSON_HEX_TAG);
    

    In JS to decode the same:

    var d = $.parseJSON(content);
    
    0 讨论(0)
提交回复
热议问题