如何使用Laravel + Jquery Ajax从textarea获取值?

时间:2021-05-13 20:29:06

How do I take value from one textarea to another with laravel and jquery ajax. I have this files so far. Route:

如何使用laravel和jquery ajax从一个textarea转换到另一个textarea。到目前为止我有这些文件。路线:

Route::post('/post', 'PostController@post');

Controller:

控制器:

class PostController extends Controller
{
       public function post(Request $request)
    {

 $request->json()->all();

    }
}

JQuery file:

JQuery文件:

$(function(){


 $('#insert').on('click', function(e){
     e.preventDefault();
 var intrare = $('textarea#firsttextarea').val();
  $.ajax({
    type:'POST',
    url: '/post',
    data: {intrare: intrare},
    success: function(data){
       $('textarea#secondtextarea').val(data);
    }

 });
 });

});

and the html :

html:

<textarea class="form-control" name="firsttextarea" rows="10" id="firsttextarea" ></textarea>

<button  id="insert" class="btn btn-md btn-primary"><span class="glyphicon glyphicon-circle-arrow-right"></span>Insert</button>

<textarea class="form-control" name="secondtextarea" rows="10" id="secondtextarea" ></textarea>

When i push the button nothing happens.

当我按下按钮时,什么也没有发生。

2 个解决方案

#1


1  

Try this, you're not returning a response in your Controller method.

尝试一下,您不会在控制器方法中返回响应。

class PostController extends Controller
{
    public function post(Request $request)
    {      
        return response()->json([
            'data' => $request->get('intrare'),
        ]);
    }
}

then, add this to your head in your blade file

然后,在你的刀片文件中添加这个到你的头部

<meta name="csrf-token" content="{{ csrf_token() }}">

and replace your JS with the below:

将你的JS替换为:

$(function() {

  // We need CSRF Token in our ajax request so we can
  // validate the POST request
  $.ajaxSetup({
    headers: {
      "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content")
    }
  });

  // Insert text from first textarea to the other textarea
  $("#insert").on("click", function(e) {
    e.preventDefault();
    var intrare = $("textarea#firsttextarea").val();
    $.ajax({
      type: "POST",
      url: "/post",
      data: { intrare: intrare },
      success: function(response) {
        $("textarea#secondtextarea").val(response.data);
      }
    });
  });

});

#2


2  

First problem probably can be in CSRF Verify. You can disable it if it so or add {{ csrf_token() }}. Then your post action should be looks like this:

第一个问题可能是CSRF验证。如果是这样,您可以禁用它,或者添加{{csrf_token()}}。那么你的发帖行为应该是这样的:

public function post(Request $request)
{
    return response()->json($request->all());
}

I checked it and it's work fine. but in textarea insert [Object object] because it JSON. You can add JSON.stringify in your Jquery script like this:

我检查过了,一切正常。但是在textarea插入[对象对象],因为它是JSON。您可以添加JSON。Jquery脚本中的stringify如下:

$(function(){
    $('#insert').on('click', function(e){
        e.preventDefault();
        var intrare = $('textarea#firsttextarea').val();
        $.ajax({
            type:'POST',
            url: '/post',
            data: {intrare: intrare},
            success: function(data){
                $('textarea#secondtextarea').val(JSON.stringify(data));
            }

        });
    });

});

#1


1  

Try this, you're not returning a response in your Controller method.

尝试一下,您不会在控制器方法中返回响应。

class PostController extends Controller
{
    public function post(Request $request)
    {      
        return response()->json([
            'data' => $request->get('intrare'),
        ]);
    }
}

then, add this to your head in your blade file

然后,在你的刀片文件中添加这个到你的头部

<meta name="csrf-token" content="{{ csrf_token() }}">

and replace your JS with the below:

将你的JS替换为:

$(function() {

  // We need CSRF Token in our ajax request so we can
  // validate the POST request
  $.ajaxSetup({
    headers: {
      "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content")
    }
  });

  // Insert text from first textarea to the other textarea
  $("#insert").on("click", function(e) {
    e.preventDefault();
    var intrare = $("textarea#firsttextarea").val();
    $.ajax({
      type: "POST",
      url: "/post",
      data: { intrare: intrare },
      success: function(response) {
        $("textarea#secondtextarea").val(response.data);
      }
    });
  });

});

#2


2  

First problem probably can be in CSRF Verify. You can disable it if it so or add {{ csrf_token() }}. Then your post action should be looks like this:

第一个问题可能是CSRF验证。如果是这样,您可以禁用它,或者添加{{csrf_token()}}。那么你的发帖行为应该是这样的:

public function post(Request $request)
{
    return response()->json($request->all());
}

I checked it and it's work fine. but in textarea insert [Object object] because it JSON. You can add JSON.stringify in your Jquery script like this:

我检查过了,一切正常。但是在textarea插入[对象对象],因为它是JSON。您可以添加JSON。Jquery脚本中的stringify如下:

$(function(){
    $('#insert').on('click', function(e){
        e.preventDefault();
        var intrare = $('textarea#firsttextarea').val();
        $.ajax({
            type:'POST',
            url: '/post',
            data: {intrare: intrare},
            success: function(data){
                $('textarea#secondtextarea').val(JSON.stringify(data));
            }

        });
    });

});