2017-07-13 50 views
0

我在laravel上創建了一個博客,到目前爲止,我已經爲包含標題和內容的帖子成功製作了js代碼。但我在爲標籤寫js函數時遇到了一些麻煩。創建用於提交按鈕的JavaScript函數以返回數據

我想爲標籤做同樣的事情,但我在嘗試的所有事情上都遇到了錯誤。

<script src="https://cloud.tinymce.com/stable/tinymce.min.js?apiKey=fg5tc8gb4rtw6p9n3njd2hi4965rketxda84pbcfs09hb5x2"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.6.4/tinymce.min.js"></script> 
 
    <script type="text/javascript"> 
 
     tinymce.init({ 
 
      selector: '.myeditablediv', 
 
      plugins: 'code , save, autoresize , textcolor colorpicker , emoticons, textpattern , wordcount', 
 
      toolbar: 'save , restoredraft , forecolor backcolor, emoticons', 
 
      save_onsavecallback: function() { 
 
       var content = tinymce.activeEditor.getContent(); 
 
      console.log(content); 
 
      } 
 
     }); 
 

 
     $(document).on('click', '#SubmitBtn', function() { 
 
      var content = tinymce.activeEditor.getContent(); 
 

 
      var data = { 
 
       'title': $('#title').val(), 
 
       'content': content, 
 
       '_token': '{{csrf_token()}}' 
 
      }; 
 
      $.post('/postData', data, function() { 
 
        console.log(data); 
 
      }); 
 
     }); 
 

 
    </script>
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
</head> 
 
<body> 
 
    <h1>Create the title</h1> 
 

 
     <form> 
 

 
      {{csrf_field()}} 
 

 
      <label for="title">Click here to edit the title of your post!</label> 
 
      <input type="text" name="title" id="title"/> 
 

 
      <h1>Create the content</h1> 
 

 
      <div class="myeditablediv">Click here to edit the content of your post!</div> 
 

 
     </form> 
 

 
     <input type="button" name="Submit" id="SubmitBtn" value="Submit"/> 
 
</body> 
 
</html>

<script src="https://cloud.tinymce.com/stable/tinymce.min.js?apiKey=fg5tc8gb4rtw6p9n3njd2hi4965rketxda84pbcfs09hb5x2"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.6.4/tinymce.min.js"></script> 
 
<script type="text/javascript"> 
 
     tinymce.init({ 
 
      selector: '.myeditabletag', // change this value according to your HTML 
 
      menu: { 
 
       view: {title: 'Edit', items: 'cut, copy, paste'} 
 
      }, 
 
      save_onsavecallback: function() { 
 
       var content = tinymce.activeEditor.getContent(); 
 
       console.log(content); 
 
      } 
 
     }); 
 

 
     $(document).on('click', '#SubmitBtn', function() { 
 
      var name = tinymce.activeEditor.getContent(); 
 

 
      var data = { 
 
       'name': name, 
 
       '_token': '{{csrf_token()}}' 
 
      }; 
 

 
      $.post('/postTags', data, function() { 
 
       console.log(data); 
 
      }); 
 
     }); 
 

 
    </script>
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
</head> 
 
<body> 
 

 
    <h1>Create a new Tag</h1> 
 

 
     <form> 
 
     {{csrf_field()}} 
 

 
      {{--<input type="text" name="name" id="name"/>--}} 
 

 
      <div class="myeditabletag">Click here to edit the name of your tag!</div> 
 

 
     </form> 
 
     <input type="button" name="Submit" id="SubmitBtn" value="Submit"/> 
 
</body> 
 
</html>

這裏是標籤和員額/ POSTDATA路線:

Route::post('/postTags', ['uses' => '[email protected]']); 
Route::post('/postData', ['uses' => '[email protected]']); 

這裏是PostController中和TagController小號tore方法:

public function store(Request $request) 
{ 
    $post = new Post; 
    $post->title = $request['title']; 
    $post->content = $request['content']; 
    $post->save(); 
} 

public function store(Request $request) 
{ 
    $tag = new Tag; 
    $tag->name = $request['name']; 
    $tag->save(); 
} 
+1

你得到什麼錯誤? –

+0

我將$ .post更改爲$ .tag,它表示$ .tag不是函數。我還將標題更改爲標籤的名稱。即使我這樣做,它仍然不會返回我的phpmyadmin上的任何數據,就像它對帖子所做的一樣。 – livia

+0

是因爲jquery不提供名爲'$ .tag'的方法?我很困惑,讓你感到困惑。 – Xatenev

回答

1

你的JS代碼有錯誤。您正在選擇一個不存在的ID。您需要選擇已更改標籤的內容,並將其作爲數據['名稱']發送。試試下面的代碼:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.6.4/tinymce.min.js"></script> 

<script type="text/javascript"> 
    tinymce.init({ 
     selector: '.myeditabletag', // change this value according to your HTML 
     menu: { 
      view: {title: 'Edit', items: 'cut, copy, paste'} 
     }, 
     save_onsavecallback: function() { 
      var content = tinymce.activeEditor.getContent(); 
      console.log(content); 
     } 
    }); 

    $(document).on('click', '#SubmitBtn', function() { 
     var name = tinymce.activeEditor.getContent(); 

     var data = { 
      'name': name, 
      '_token': '{{csrf_token()}}' 
     }; 

     console.log(data); 
     $.post('/postTags', data, function() { 

     }); 
    }); 

</script> 
+0

我也嘗試過,但它仍然不起作用 – livia

+0

你可以添加更多的細節到您的文章?錯誤消息等,以便我們可以排除故障? –

+0

我正在監視控制檯上的錯誤。我相信這是VAR數據 POST http://blog.app/postData 500(內部服務器錯誤) 發送@ jquery.min.js:4個 AJAX @ jquery.min.js:4 R(匿名函數)@ jquery.min.js:4 (匿名)@ tag:29 dispatch @ jquery.min.js:3 q.handle @ jquery.min.js:3 – livia