“Unprocessable Entity”通过Ajax将数据传递给Laravel控制器

时间:2018-06-08 20:01:37

标签: ajax laravel

我正在尝试使用Ajax向Laravel Controller提交数据,但我收到错误“422(Unprocessable Entity)”。

我做了一些谷歌搜索,我认为这是通过传递的JSON,但我不确定如何解决它。

以下是我认为的相关信息:

脚本

import csv
from dateutil.parser import parse
import pandas as pd
import time
from datetime import datetime


path = r'C:\Users\Ahmed Ismail Khalid\Desktop\test\_jonasschnelli__tweets.csv'


with open(path,'rt',encoding="utf-8") as f :
reader = csv.reader(f)
for row in reader :
   print(row[1])
   print("Converted time is :", datetime.strptime(row[1], '%d/%m/%Y %H:%M:%S'))

id | created_at | text 1 | 1/15/2018 6:12:16 AM | this is sample text 2 | 1/11/2018 6:58:27 AM | this is sample text 3 | 1/10/2018 9:39:33 PM | this is sample text 4 | 1/10/2018 8:47:35 PM | this is sample text 提供$("#addStepNew").click(function() { var step_ingredients = JSON.stringify(stepIngredients) var step_description = $('#stepDescription').val(); var prep_step = $('input[name=prepStep]:checked').val(); $.ajax({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, type: "post", data: "ingredients="+step_ingredients+"&description="+step_description+"&is_prep="+prep_step+"step_no=1", dataType:'json', url: "{{ route('ingredients.store', ['id' => $recipe->id]) }}", success:function(data){ console.log(data); $("#output").html('<div class="alert alert-success my-0">'+data.name+' added</div>'); $("#output").toggleClass("invisible") $("#output").fadeOut(2000); } }); });

我的想法是将它传递给我的控制器,并将值写入数据库,但是在那一刻我甚至无法传递信息。

我已经向控制器完成了以下操作:

console.log(stepIngredients)

据我所知(我正在自学这个),如果AJAX成功通过,那么它应该返回成功消息并将其输出到我的[{"ingredient_id":"9","ingredient_quantity":"3","Ingredient_units":"kilograms"}]中?

public function store(Request $request)
{
  //$this->validate($request);

  /*
  $step = new Step;
  $step->recipe_id = $request->recipe_id;
  $step->step_no = $request->step_no;
  $step->method = $request->description;
  $step->save();
  */

  $data = [
    'success' => true,
    'message'=> 'Your AJAX processed correctly',
  ] ;

  return response()->json($data);
}

0 个答案:

没有答案