I'm pushing an ajax post with jquery to e method in the controller which should return a json response but I'm not getting anything, no errors but no response either. Then end goal is broader as you will see from my ajax but for now getting that response back from the controller would be stellar
The jquery ajax method:
$.ajax({
url : "/menu/sort",
type : 'POST',
data : { menu: menu, order: order },
success: function (data) {
holder.html(data);
}
});
The controller action:
return Response::json('ok');
my route:
Route::post('menu/sort', 'MenuController#sort');
Related
I wanted to submit a for using ajax call in laravel 5.
In view i wrote something like
$("#updateSubmit").on('submit',function(e){
e.preventDefault();
var csrfToken = $('meta[name="csrf-token"]').attr("content");
$.ajax({
method:'POST',
url: '/account/updateForm',
//dataType: 'json',
data: {accountId:'1111', _token: '{{csrf_token()}}'},
success: function( data )
{
alert(data)
return false;
}
},
error: function(error ){
alert("There is some error");
}
});
and on controller side
public function update(Request $data )
{
return Response()->json(['success' => true],200);
}
while in route for post method
Route::post('account/updateForm', 'AccountController#update')->name('account/updateForm');
its working till ajax. on Submission of ajax it goes to controller action.
but it does not retrun back as ajax comes back in normal form submisson.
it just go to controller and stops there with {"success":true} line.
I want ajax to come back to view form so that I can perform different dependent actions.
Do you mean that when you submit your form, you just have a white page with {"success": true} ?
If that's the case, maybe the error is on your javascript.
Maybe your jQuery selector is wrong, or maybe your js isn't compiled ?
I am working on a Post Ajax request Function. Where the function takes some data and send it through an enabled CSRF_token post Request to a controller and then after evaluations on controller a message sent back to the view. but it seems i miss a small thing in my code.
My controller
public function PostMessage(Request $request){
$message=$request->someData; //getting data from request variable
return response()->json($message);
}
My jquery Ajax request function
$('.SendAjaxPostRequest').on('click', function() {
var value=$('.MessageHolder').val();
$.ajax({
method: 'POST',
url:'{{route('SVCate')}}', //SVCate is my route to the controller
dataType: 'JSON',
data: {_token:token,'someData':value,}
// #token gets it's value from a local view javaScrip Variable
})
.done(function (data) {
console.log(data);
})
});
My route function
Route::post('SendMessage','NessageController#PostMessage')->name('SVCate');
Check your apostrophes in the url. You are ending the string and beginning the string around SVCate change it to url:"{{route('SVCate')}}" to make sure that SVCate stays a string and does not break your string.
I want to send data from HTML view to Django backend to process and then the backend will send an url back the to HTML view which will redirect the user to another page. It's like this:
Page 1 ----user's input data --- Ajax --- > backend --- process --- a url to page 2 --- Ajax --> page 2
The problem is that, I don't know how to send the URL back Ajax after processing user's data, so I have to redirect by using window.location.href = '/' . But I think the code is not clean this way. I wonder if there is a way to send url to Ajax's success from backend.
Here is the code n the HTML :
function doSomething(data){
$.ajax({
type: "POST",
url: URL_POST_BY_AJAX,
data: {
'data' : data,
},
success: function (response) {
window.location.href = '/';
},
error: function (data) {
alert('failed');
}
});}
Please help me. Thank you!
In your processing view:
from django.http.response import JsonResponse
def whatever_your_view_name_is(request):
(data processing...)
return JsonResponse({'url': the_url_to_page_2)
then in your JS:
(...)
success: function (response) {
window.location.href = response.url;
},
(...)
I am currently creating an AJAX call which queries a controller and returns the appropriate reponse. The only issue is is that the response is coming back as undefined doe to the async nature of the AJAX cal. I am unsure as to how I tell the function to wait for the response. Here is my code:
View:
jQuery(document).on("click", "#payment .membership", function(e) {
e.preventDefault();
var price = SignUpObject.membershipClick(jQuery(this).attr("data-membership-id"));
alert(price);
});
Javascript Library Function (which is within an object):
var SignUpObject = {
membershipClick : function(membershipDetailsId) {
jQuery.ajax({
type : 'POST',
dataType : 'json',
url : 'api/membership-choice',
data : 'membershipid=' + membershipDetailsId
}).done(function(response) {
return response
});
}
}
The PHP that the AJAX call is calling returns the correct response back so I don't need to include them here. Can anyone tell me how to make the AJAX call wait for a response?
Thanks
You've got two problems:
1) You're attempting to call the response synchronously, before the (asynchronous) request has completed.
2) membershipClick does not return the request object, so you've got no means of hooking a completion callback onto it.
To fix:
1) Change the line
jQuery.ajax({...
to
return jQuery.ajax({
2) Change the line
alert(price);
to
price.done(function(response) { alert(response); });
However, the variable price would be better named something like price_request, since it stores a reference to the request, not the actual price (which is the response.)
Change
}).done(function(response) {
return response
});
For:
}), success: function(response) {
return response
};
I have a form in my ASP.NET MVC 3 application that will use jquery to send it's data to the controller action instead of doing a normal postback:
$('.AjaxForm').live("submit", function (event) {
event.preventDefault();
$.validator.unobtrusive.parseDynamicContent('.uiModalContent input');
console.log($(this).attr('action'));
$.ajax({
type: 'POST',
url: $(this).attr('action'),
data: data,
success: function (responseHtml) {
alert(responseHtml);
},
error: function (responseHtml) {
alert('error');
},
statusCode:
{
404: function (responseHtml) {
alert('404');
},
500: function (responseHtml) {
alert('500');
}
}
});
});
However I get an error saying data is undefined... How do I get the data from the form and send it? Also does the built-in validation in ASP.NET MVC 3 work with my code or will I have issues? Thanks
You should use form.serialize() to make data value. Your code should be changed to:
url: $(this).attr('action'),
data: $('.AjaxForm').serialize(),
you can use the serialize or serializeArray method with form to pass all parameters with the ajax call.
data: $('.AjaxForm').serialize(),
http://api.jquery.com/serialize/
http://api.jquery.com/serializeArray/