I've tried to send data to "plain" php and it works like i expected, however I'm new to laravel and still can't call specific method using js ajax.
I'm trying to send data using ajax post method and call the specific method of laravel controller.
So, my javascript is outside of my laravel folder, and in the same directory "level" with my html,css and laravel folder.
Here is my function in js looks like
function addNameThroughtLaravelController(){
var req = new XMLHttpRequest();
req.open("POST", "laravel/public/product", false);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.send("Name=Jeremy");
}
My routes.php inside laravel folder looks like this
Route::get('/', [
'as' => '/',
'uses' => 'PagesController@getIndex'
]);
Route::post('laravel/public', 'ProductController@index');
Route::resource('product', 'ProductController@index');
and for my ProductController.php inside laravel controller folder
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
use DB;
use AppProduct;
use resourcesviewsproducts;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
echo "<script>console.log('index')</script>";
$product = DB::select('select * from feedback');
return view('products.index')
->with('product',$product);
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
return view('products.create');
}
/**
* Store a newly created resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function store(Request $request)
{
/*$rating = new Product;
$rating->Name= $request->name;
$rating->avg=$request->price;
$rating->save();*/
$inputs= $request->all();
$product= Product::create($inputs);
//return redirect()->route('product.index');
return redirect()->action('ProductController@index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function show($id)
{
echo "<script>console.log('tes')</script>";
//'$product= Product::where('idoveralRating',$id)->first();
$product = Product::find($id);
echo "<script>console.log('yes')</script>";
//return $product;
return view('products.show')
->with('product',$product);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function edit($id)
{
//
// $product= Product::all();
$product= Product::where('idoveralRating', $id)->first();
return view('products.edit')
->with('product',$product);
}
/**
* Update the specified resource in storage.
*
* @param IlluminateHttpRequest $request
* @param int $id
* @return IlluminateHttpResponse
*/
public function update(Request $request, $id)
{
//
$product = Product::find($id);
$product->Name = $request->Name;
$product->Avg = $request->Avg;
$product->save();
$product = Product::find($id);
return redirect()->route('product.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function destroy($id)
{
$product= Product::find($id) ->delete();
return redirect()->route('product.index');
}
}
There is no error showed , meanwhile i cant figure it out what url should i use in my javascript so i can call to call specific method inside my controller , I've tried to send it to routes.php inside my laravel folder and still didnt work, send it to laravel/public also and still didnt work.
I think the problem is only related to url in javascript post method, because when i tried to access localhost http://localhost/User/laravel/public/product, it print to console index
The output that i expected is clearly that server(localhost) should call the ProductController@index, which should print string index on my console(im using Google chrome for my browser)
some important thing that might be notice
Laravel folder path: C:xampphtdocsUserlaravel
routes.php path: C:xampphtdocsUserlaravelapproutes.php
ProductController.php path: C:xampphtdocsUserlaravelappHttpControllersProductController.php
JS path : C:xampphtdocsUser
What am I doing wrong?
Aucun commentaire:
Enregistrer un commentaire