AJAX - 将html代码插入数据库

时间:2017-01-19 15:41:11

标签: jquery ajax laravel

我正在从我的网站检索html代码,我需要将其存储到数据库中。要检索的代码如下所示:

document.getElementById("content-link2").onmousedown = function() {mousedown()};
document.getElementById("content-link2").onmouseup = function() {mouseup()};
function mousedown() {
   var code2 = document.getElementById("content-link2").innerHTML;
    console.log(code2);
}
function mouseup() {
   var code2 = document.getElementById("content-link2").innerHTML;
    console.log(code2);
}

输出是:

    <title>Template 1</title>
        <link href="http://localhost/fyproject/public/templates/css.css" rel="stylesheet" type="text/css">


    <div class="logo">
        <img class="images" id="image" src="#" alt="Your Logo">
    </div>
<div contenteditable="true" id="content" class="draggable ui-widget-content refresh ui-draggable ui-draggable-handle" style="position: relative;"><p>hlo</p></div>
<div id="comments">
<form name="forma">
<textarea name="commentUser" id="commentUser" class="refresh" cols="40" rows="5">Comments here...
</textarea><br>
<input type="submit" value="Ready!" id="send-data"><!--onClick="writeComment(e);"--> 
<div id="editTxt" class="refresh" contenteditable="true">
<p>This text can be by the user.</p>
</div>
</form></div>

我已经开始执行保存在js文件中的AJAX代码,它看起来像:

 function updateDatabase(newCode)
            {

            // make an ajax request to a PHP file
            // on our site that will update the database
            // pass in our lat/lng as parameters
            $.post('http://localhost/fyproject/public/template2', {
                 _token: $('meta[name=csrf-token]').attr('content'),
                 newCode: code2,
             }
            )
            .done(function(data) {
                alert(data);
            })
            .fail(function() {
                alert( "error" );
            });
        }

路线:

Route::post('template2', 'BuilderController@postDB');

控制器:

     public function postDB(Request $request) {
        $newLat = $request->input('newCode');

        return "Is This Really Working? Oh by the way: newLat: $newLat";
    }

但是我在控制台中收到此错误:

  

POST http://localhost/fyproject/public/template2 500(内部服务器   误差)

我是否走上正轨?我在这做错了什么?如果这不好,我能否以其他方式提出如何做到这一点的建议?

谢谢

0 个答案:

没有答案