如何使用ajax将数据作为参数传递给控制器​​?

时间:2017-01-02 12:25:04

标签: php ajax laravel-5

我想将Array传递给Controller,但我无法提供正确的URL 当点击提交404错误

我的路线

Route::post('store/{data}','DataController@store');

功能

    public function store(Request $request)
{
        dd($request);

    return response()->json([
        'success' => true,

    ]);

}

Ajax

     $.ajax({
            method:'POST',
            url:"/store/"+arr,
            headers: {
                'X-CSRF-TOKEN': CSRF_TOKEN
            },
            data:arr,
            success:function(response){
            alert('successful')                }
        });

和错误

    POST http://localhost:8000/store%7Bname,number,number,address,work,$2y$10$Nl28n1pWCEaHxiHlcG.Y1On2DOdtXluHP0OvN5ih.Bvdfp/Issco2%7D 404 (Not Found)

4 个答案:

答案 0 :(得分:0)

根据你的路线,ajax调用应该是这样的:

   $.ajax({
            method:'POST',
            url:"/data/"+arr . . . 

或者您可以更改您的ajax工作路线:

Route::post('store/{data}','DataController@store');

答案 1 :(得分:0)

public static void main(String[] args) throws Exception {

    Configuration configuration = new Configuration();
    configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us"); 
    configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");        
    configuration.setGrammarPath("file:src");
    configuration.setGrammarName("hello");
    configuration.setUseGrammar(true);

    LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);

    recognizer.startRecognition(true);

    SpeechResult result;
    while ((result = recognizer.getResult()) != null ) {
        System.out.format("The word is: %s\n", recognizer.getResult().getResult().toString());
    }

    recognizer.stopRecognition();
}

ajax应该是这样的:

There is no need for {data}

答案 2 :(得分:0)

尝试 路线::得到( '存储/ {数据}', 'DataController类@商店');

并将ajax方法更改为GET

答案 3 :(得分:0)

$ request变量始终在控制器方法中可用。添加url参数时,还应将其定义为函数参数($ data)     

public function store(Request $request, $data)
{
    dd($data);
    return response()->json([
        'success' => true,

    ]);
}