I have strange problem because previously in my app this code was working but now isn't.
I take data from Laravel api via url address:
/api/customer/{id}/products
to select2 script
$('.js-data-example-ajax').select2({
ajax: {
url: '/api/customer/{id}/products',
dataType: 'json',
data: function (params) {
var query = {
q: params.term,
}
return query;
}
but Laravel make url address i that way:
api/customer/%7Bid%7D/products
so, I have %7Bid%7D instead of {id} and I'm looking for solution in google without success.
The character "7B" is { converter to asci, before the ajax request create a var call "url"
i call the route with his name, for give it the name just attach
->name('your_name') in the route file
and after in url variable i use the route name instead the full url
url = '{{ route("your_route_name", ":id") }}';
then replace the id placeholder with the id of select
url = url.replace(':id', id);
finally in ajax request
ajax: {
url: url,
//the rest of ajax request
}
Related
I am trying to post data to a controller through ajax request. But it can't find the route and says the following in console.
POST http://127.0.0.1:8000/addNotification_action 404 (Not Found)
This is my ajax call below.
function editNotification(obj) {
// alert(obj.id);
var obj_id = obj.id;
var id = obj_id.split("_");
$.ajax({
url: "{{ url('addNotification_action') }}",
type: 'POST',
dataType: 'json',
data: {edit_notification_id: id[1]},
})
.done(function(result) {
console.log(result);
$('#title').val(result['title']);
$('#description_notification').val(result['details']);
$('#edit_flag_notification').val(result['notification_id']);
})
.fail(function() {
alert("error");
});
}
And I am just trying to dd() the request I get in the controller. Please help. Thanks
First of all, you should create a route and give this route a name. You have to generate the ajax url as "route('route_name')".
// Route (for Laravel 8)
Route::post('addNotification_action', [NotificationController::class, 'notify_method'])->name('notify');
// Ajax Settings
....
url: "{{ route('notify') }}",
....
Jus Give admin/addNotification_action in Ajax URL
I am working on Jquery AJAX in OXID eSHOP.
I want to pass proper action (function) name in AJAX url parameter.
Below is my code:
jQuery.ajax({
type: "POST",
data: {
variable: value
},
url: "",
success: function(data) {
}
});
I want to call one function in url parameter.
But don't know how to pass its value in url paramaeter.
Anyone knows then please help me.
You should include function name as URL parameter, just like the shop does it in most situations:
http://your-shop.com/index.php?cl=controller&fnc=functionname
Unable to create Delete action in Laravel.
I am getting Not Found or Token mismatch errors all the time.
My controller:
class TranslationController extends Controller
{
public function destroy($id)
{
//$id = 1;
/*$translation = Translation::find($id);
$translation->delete();*/
}
....
}
Ajax call:
/* Delete given translation */
var url = "translation";
var id = 1;
$.ajax({
method: 'DELETE',
url: url + '/' + id,
// data: {'id': id, '_token': token},
success: function() {
}
});
This would give: TokenMismatchException in VerifyCsrfToken.php line 53:
If I try:
url: url + '/' + id,
data: {'_token': token}, // token is equal to csrf_token
I have: NotFoundHttpException in Controller.php line 269:
Routes:
Route::controller('translation', 'TranslationController');
Otherwise I am using Laravel 5 default Middleware, I have not changed anything related to csrf.
NotFoundHttpException means that either the route for the particular request with the particular HTTP verb has not been specified, or the action (i.e. the controller method) that is mapped to the verb for the route is wrongly implemented.
Since you've mentioned in the post that the TranslationController is defined as an implicit controller,
Route::controller('translation', 'TranslationController');
and from the controller code you've posted, it's quite obvious that you have not defined the verb for the destroy method in your controller TranslationController.
If you do a php artisan route:list in your projects root directory with a terminal/command line interface, you'll see the listing of the registered HTTP verbs, mapping to the corresponding URIs, and the actions.
To define a particular method in an implicit controller, the verb (GET, PUT, POST, DELETE) should precede the actual function name.
Make sure that the destroy method looks like the following in your controller:
public function deleteDestroy($id){
//delete logic for the resource
}
Note:
Laravel by default requires that the csrf token is passed along with a particular RESTful request, so do not remove data: {'_token': token} from your AJAX call.
Update
Forgot to mention that the url in your AJAX call should also be changed to the following in order to work, because this is how Laravel's implicit controllers define the route for a DELETE request:
var url = "translation/destroy";
Here is documentation about method spoofing. You need to send a POST ajax request with _method field set to DELETE
$.ajax({
method: 'POST',
url: url + '/' + id,
data: {
'id': id,
'_token': token,
'_method' : 'DELETE'
},
success: function() {
}
});
You could try defining your route that way
Route::delete('translation/{id}',array('uses' => 'TranslationController#destroy'));
In this case your AJAX won't change. But if you want keep this Route
Route::controller('translation', 'TranslationController');
You must change your Ajax request to:
/* Delete given translation */
var url = "translation/destroy"; // You must specify the action
var id = 1;
$.ajax({
method: 'DELETE',
url: url + '/' + id,
data: {'_token': token},
success: function() {
}
});
You should to send token via header. ( especially in 5.2 version )
$.ajax({
type: "post",
url: "/routeurl",
headers: { 'X-CSRF-Token': "{!! csrf_field() !!}" },
success: function(msg){
// msg
}
});
I am trying to do a simple call to my controller via ajax. The RouteConfig has not been changed and set to the default values. When I make ajax call, the Url that is requested in the "Network" debugging tools is:
'http://localhost:59275/Leaderboard/Leaderboard/GetPosition'
This is causing a 404 because the Controller, Leaderboard, is being added into the Url twice. The correct url should be
'http://localhost:59275/Leaderboard/GetPosition'
My ajax call is as follows:
$.ajax({
url: 'Leaderboard/GetPosition',
type: "GET",
dataType: 'xml',
success: function (data) {
$('#results').html(data);;
}
});
and my controller is as follows:
public class LeaderboardController : Controller
{
[Webmethod]
public static DataTable GetPosition()
{
// do stuff
}
}
If the root URL of the page that request the ajax is "Leaderboard" then the url on the ajax call should only "GetPosition"
Or you can use "/Leaderboard/GetPosition" with "/" in front
Use Url.Action helper for generating correct url.
Change this:
url: 'Leaderboard/GetPosition'
to this:
url: '#Url.Action("GetPosition","Leaderboard")'
I want to take URL parameter and pass to ajax function,
var queryParams = dojo.queryToObject(window.location.search.slice(1));
var data = dojo.toJson(queryParams, true);
console.log(data);
getCategory( data );
...
function getCategory(input){
dojo.xhrGet( {
// The following URL must match that used to test the server.
url: "http://js.localhost/dojo-release-1.5.0-src/json3.php",
handleAs: "json",
content: input,
on my URL parametetr, I pass in
?return_type=category&category_desc=Business2
when I view on firebug, the ajax request become ....
t?0=%7B&1=%0A&2=%09&3......
but data debug is correct, any idea what's wrong?
{ "return_type": "category",
"category_desc": "Business2" }
just found out that it doesn't need dojo.toJson
var queryParams = dojo.queryToObject(window.location.search.slice(1));
getCategory( queryParams );