Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

jquery - how to setup return for ajax

this is request form front end

<script type="text/javascript">
//alert("jquery running1");
    $(document).ready(function($){
    MyOtherServiceadds();
    
    function MyOtherServiceadds(query = '') {
    //alert(query);  

    $("#addlist").empty();
        $.ajax({
        url: 'TobeSubmit',
        type: 'GET',
        datatype:'json',
        data:{SearchKey:query},
        })
        .done(function(data) {
            alert("done");
            $.each(data, function(index, val) {
                var Link ="<a target='_blank' href='/openAgntApp/"+val.id+"' >{{ __('Open') }}</a>";  

            $('#addlist').append('<tr>')
            $('#addlist').append('<td>'+val.RefId+'</td>')
            $('#addlist').append('<td>'+val.Category+'</td>')
            $('#addlist').append('<td>'+val.ContactNo+'</td>')
            $('#addlist').append('<td>'+val.Status+'</td>')
            $('#addlist').append('<td>'+val.Post_Date+'</td>')
            $('#addlist').append('<td>'+Link+'</td>')
            $('#addlist').append('</tr>')

            });
        })
        .fail(function() {
            alert("fail")
            console.log("error");
        })
    
    }
    $("#UnderReview").click(function(){ var query = $("#UnderReview").val();  MyOtherServiceadds(query);  });
    $("#Submited").click(function(){ var query = $("#Submited").val();  MyOtherServiceadds(query);  });
    $("#Rejected").click(function(){ var query = $("#Rejected").val();  MyOtherServiceadds(query);  });
    $("#Expired").click(function(){ var query = $("#Expired").val();  MyOtherServiceadds(query);  });
    $("#Deleted").click(function(){ var query = $("#Deleted").val();  MyOtherServiceadds(query);  });
    $("#search").click(function(){ var query = $("#searchKey").val();  MyOtherServiceadds(query);  });

    //$(document).on('keyup', '#search', function(){
});
</script>

this is controller

public function tobeSubmit() { return adds::orderBy('id','desc')->get(); }

according to this setup , query running well and data receiving , but I want to search key words in database and execute so I Use this code. ( this code is working in another my site} but it not working here, can you anyone tell me how to solve that issue

public function tobeSubmit()
{
    if($request->ajax())  {
        $SearchData = $request->get('SearchKey');
          if($SearchData !=''){
            return adds::orderBy('id','desc')->get(); 
              /*return adds::where('Status','like','%'. $SearchData.'%' )
              ->orWhere('RefId','like','%'. $SearchData.'%')
              ->orWhere('Category','like','%'. $SearchData.'%')
              ->orWhere('ContactNo','like','%'. $SearchData.'%')
              ->orderBy('id','desc')
              ->get();*/
            }  
          else { 
            return adds::orderBy('id','desc')->get();  }
     } else { return 'false'; }
     
}
question from:https://stackoverflow.com/questions/65598598/how-to-setup-return-for-ajax

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Problem:


You have not declared $request variable in tobeSubmit() function.

Solution:


You just use IlluminateHttpRequest param for tobeSubmit().

     /**       
     *
     * @param  IlluminateHttpRequest $request
     * @return Response
     */
    public function tobeSubmit(Request $request)
    {
       ...
    }

Note: Don't forget to declare the Request class at the top of the file.

use IlluminateHttpRequest;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...