Ajax Laravel 5.2不起作用


                    

我想用laravel5.2用此代码开发简单的ajax </ p>

此oneline_help.php视图</ p>

  
     <头>
       Ajax示例</ title>

&lt;/ script&gt;<br>        &lt;脚本&gt;<br>           函数getMessage(){<br>              $ .ajax({<br>                 输入:“ POST”,<br>                 网址:“ / getmsg”,<br>                 数据:“ _ token = &lt;?php echo csrf_token()?&gt;”,<br>                 成功:功能(数据){<br>                    $(“#msg”)。html(data.msg);<br>                 }<br>              });<br>           }<br>        &lt;/ script&gt;</p> <p>&lt;/ head&gt;<br> &lt;身体&gt;<br>        <div id ='msg'>此消息将使用Ajax替换。<br>           单击按钮替换消息。&lt;/ div&gt;<br>        &lt;?php<br>           echo Form :: button(&#39;替换消息&#39;,[&#39;onClick&#39;=&gt;&#39;getMessage()&#39;]);<br>        ?&gt;</p> <p>     &lt;/ body&gt;</p> <p>&lt;/ html&gt;<br> &lt;/ code&gt; &lt;/ pre&gt;</p> <p><p>这是路线&lt;/ p&gt;</p> <p><pre> <code> Route :: get(&#39;/ ajax&#39;,&#39;front @ support&#39;);<br> 路线:: post(&#39;/ getmsg&#39;,&#39;Hello @ index&#39;);<br> &lt;/ code&gt; &lt;/ pre&gt;</p> <p><p>这是前面的@support &lt;/ p&gt;</p> <p><pre> <code>公共功能support()<br> {<br>      return view(&#39;online_help&#39;,array(&#39;title&#39;=&gt;&#39;Welcome&#39;,&#39;description&#39;=&gt;&#39;&#39;,&#39;page&#39;=&gt;&#39;online_help&#39;,&#39;subscribe&#39;=&gt;“”,&#39;brands =&gt; $ this -&gt;品牌));<br> }<br> &lt;/ code&gt; &lt;/ pre&gt;</p> <p><p>这是Hallo @index控制器&lt;/ p&gt;</p> <p><pre> <code>公共功能index(){<br>         回显“我在你好索引中”;<br>        $ msg =“这是一条简单的消息。”;<br>        返回response()-&gt; json(array(&#39;msg&#39;=&gt; $ msg),200);<br>     }<br> &lt;/ code&gt; &lt;/ pre&gt;</p> <p><p>出现按钮,但是单击它时,文本不会改变。<br> 请告诉我原因以及解决方法。&lt;/ p&gt;<br>      &lt;/ div&gt;</p>

展开原文

原文

I want to develop simple ajax with laravel5.2 with this code

This oneline_help.php view

<html>
    <head>
      <title>Ajax Example</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script>
         function getMessage(){
            $.ajax({
               type:'POST',
               url:'/getmsg',
               data:'_token = <?php echo csrf_token() ?>',
               success:function(data){
                  $("#msg").html(data.msg);
               }
            });
         }
      </script>

</head>
<body>
      <div id = 'msg'>This message will be replaced using Ajax. 
         Click the button to replace the message.</div>
      <?php
         echo Form::button('Replace Message',['onClick'=>'getMessage()']);
      ?>

    </body>

</html> 

This is the routes

Route::get('/ajax','front@support');
Route::post('/getmsg','Hello@index');

This is front @support

public function support() 
{
    return view('online_help', array('title' => 'Welcome', 'description' => '', 'page' => 'online_help','subscribe'=>"",'brands' => $this->brands));
}

This is Hallo @index controller

public function index(){
       echo"i in in hello index";
      $msg = "This is a simple message.";
      return response()->json(array('msg'=> $msg), 200);
   }

The button appear But when click on it, The text doesn't change . Please tell me why and how to resolve it.

1个回答


这是我的AJAX工作示例。...</ p>

您不需要在ajax请求中添加 token </ code>,而是将其设置为全局,因此在每个ajax请求中,您的 CSRF令牌</ code>都会自动添加。</ p >

在您的 HTML Head </ strong>部分中添加此元标记</ p>

  <元名称=“ csrf-令牌” content =“ <?php echo csrf_token()?>”>
</ code> </ pre>

然后将此代码添加到您的Javascript标签中。 这将在每个ajax请求中添加CSRF令牌。 </ p>

注意</ strong>,此必需的jquery文件应包含在您的页面中。 一旦包含,请从文件下面调用此方法。</ p>

  <br>
     var csrf_token = $(&#39;meta [name =“ csrf-token”]&#39;)。attr(&#39;content&#39;);<br>
     $ .ajaxSetup({<br>
        标头:{“ X-CSRF-TOKEN”:csrf_token}<br>
     });<br>
 &lt;/ script&gt;<br>
 &lt;/ code&gt; &lt;/ pre&gt;</p>

<p><p> <strong>我已经修改了您的方法。...&lt;/ strong&gt; &lt;/ p&gt;</p>

<p><pre> <code> <script><br>
      函数getMessage(){<br>
         $ .ajax({<br>
            输入:“ POST”,<br>
            网址:“ / getmsg”,<br>
            data:&#39;_ token = &lt;?php echo csrf_token()?&gt;&#39;,//删除此行<br>
            dataType:&#39;json&#39;,//您跳过了这一行<br>
            beforeSend:function(){<br>
               alert(&#39;正在加载....&#39;);  //这将显示加载警报<br>
            },<br>
            成功:功能(数据){<br>
               $(“#msg”)。html(data.msg);<br>
            },<br>
            错误:function(){<br>
               警报(&#39;载入错误...&#39;)<br>
            }<br>
         });<br>
      }<br>
   &lt;/ script&gt;<br>
 &lt;/ code&gt; &lt;/ pre&gt;<br>
     &lt;/ div&gt;</p>

展开原文

原文

Here is my working example of AJAX....

You don't need to add token in your ajax request, make it global so with every ajax request, your CSRF token will be added automatically.

In your HTML Head section add this meta tag

<meta name="csrf-token" content="<?php echo csrf_token() ?>">

Then add this code in your Javascript tags. this will add CSRF token in every ajax request.

Note this required jquery file should be included in your page. once include call this method below from the file.

<script type="text/javascript">
    var csrf_token   =   $('meta[name="csrf-token"]').attr('content');
    $.ajaxSetup({
       headers: {"X-CSRF-TOKEN": csrf_token}
    });
</script>

I have modified your method....

  <script>
     function getMessage(){
        $.ajax({
           type:'POST',
           url:'/getmsg',
           data:'_token = <?php echo csrf_token() ?>', //remove this line
           dataType:'json', // you skipped this line
           beforeSend:function(){
              alert('loading....'); //this will show loading alert
           },
           success:function(data){
              $("#msg").html(data.msg);
           },
           error:function(){
              alert('loading error...')
           }
        });
     }
  </script>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐