【问题标题】:Unable to get data from db using jquery无法使用 jquery 从 db 获取数据
【发布时间】:2017-11-17 15:09:33
【问题描述】:

我在单击时从 db 检索数据并将该数据放入引导模式进行编辑时遇到问题。它在我在 db 中有字符串的地方工作,但是当数组或我有包含 html 内容的字段时,它无法加载。

这里是代码

    foreach ($unit_contents as $unit_content) {
         $default = $unit_content->is_default == 1 ? '(Default)' : '';
        $unit_content_html = $unit_content->content_html;

       <div style="height:40px;"><div class="block-options pull-right">
         <a href="#unit-of-content" data-toggle="modal" onclick="EditBlock('{{$unit_content->id}}', 
'{{$unit_content->label}}', 
'{{$unit_content_html}}')" data-toggle="tooltip" title="Edit" class="btn btn-xs btn-default"><i class="fa fa-pencil"></i>
        </a></div>
     {{$unit_content->label}} {{$default}}
     </div>;
        }

jQuery

function EditBlock(id, label, unit_content_html)
    {
        alert(unit_content_html);
        $('#label').val(label);
        $('#content_html').val(unit_content_html);
    }

我使用警报对其进行测试,它没有为 unit_content_html 提供数据,而我在 db 及其 as 中有数据

列数据

<html>
<head>
    <title></title>
</head>
<body></body>
</html>

我想在 CKEDITOR 中加载这些数据;

控制台上的错误是

未捕获的 SyntaxError:无效或意外令牌

如果我将列数据更改为字符串,它会在警报中提供数据,但不会在 CKEditor 中推送该数据。

请帮忙解决谢谢

【问题讨论】:

    标签: php jquery twitter-bootstrap laravel ckeditor


    【解决方案1】:

    尝试使用以下代码。并以json格式从服务器端获取数据 function EditBlock(id) { $.ajax({ url: "/path/to/file?id=" + id, type: "GET", dataType: "JSON", success: function (response) { $('#label').val(response.label); CKEDITOR.instances.editor1.setData(response.html_content); }, }); }

    【讨论】:

    • 感谢您的回答我正在尝试
    • 从控制器我应该返回响应还是 json 还是只返回?
    • 返回json响应return response()-&gt;json($dataArray);
    • 亲爱的,它非常有帮助,我接近解决方案如果我有这样的数组$content = $unit_contents-&gt;toJson(); $arary =[ 'form' =&gt; $content, 'html' =&gt; 'test' ]; return $arary;我如何在我的视图中使用表单作为 response.form.label 或其他东西?
    • 试试这个$arary = ['form' =&gt; "Content", 'html' =&gt; 'test']; return response()-&gt;json($arary);并在ajax响应中使用response.formresponse.html
    猜你喜欢
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 2012-09-21
    相关资源
    最近更新 更多