play framwork JsRoutes ajax form submit - ajax

im trying to submit a form via JsRoutes and ajax and i dont think that im getting to the controller ....
this is my routes
POST /submit controllers.Application.submit()
and this is my jsrouts function:
$('#submit').click(function() {
JsRoutes.controllers.Application.submit().ajax({
data : $("#contact-form").serialize(),
url: '#{Application.submit()}',
success : function(data) {
alert("Succsses");
},
error : function(err) {
alert("error");
}
});
});
this is what im sendig to the server
http://localhost:9000/?name=ajax&lastName=aaa&email=miko5054%40hotmail.com&phone=453534
what im doing wrong here ???

The #{Application.submit()} code is incorrect. It should be #{routes.Application.submit()}.
But if you use the javascript router JsRoutes.controllers.Application.submit(), you don't need to specify the url inside the ajax() call. You can just remove the url:... line and everything should work fine.

Related

ajax request is not returning back to view in laravel 5

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 ?

how to pass data to laravel route using ajax call?

i am making a facebook like kind of thing and i am using ajax for this in which in ajax data the id of the post or photo will pass and it will be of type post and in route will go to controller for updation but for just testing now in route it will return any message or the id
the ajax part(javascript)
$.ajax({
url:"vote",
type:"post",
dataType:"text",
data:{id:x},
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
},
success:function(data){
console.log(data+"sucees");
}
the php part(route part in web.php)
Route::post('/vote',function(){
if(Request::ajax()){
return Request::id();
}else{
Return "fail";
}
});
the error i am getting in console log
POST http://localhost:8000/vote 500 (Internal Server Error)
but whenever i am doing this
Route::post('/vote',function(){
if(Request::ajax()){
return Request::id();
}
it is correctly returing the data in json format
console log
{"id":"17"}sucees
help me anyone.

POST Django form with AJAX and JavaScript

I would like to get some data within a Django form
In order to do that, I define a javascript which is:
$(document).ready( function() {
$('#newcase_form').on('change', function() {
pathology_type = ($('input[name="pathology_type"]:checked','#newcase_form').val());
console.log(pathology_type);
$.ajax({
url:"/pathology/",
type :'POST' ,
data : {'pathology_type' : pathology_type},
success : function(data){
console.log(date.resultat);
}
});
});
});
It works, I can retrieve the parameter inside the form
but I am unable to post it in the URL i always have an Error 500 and the paramater is not send to the URL .
here is my URL.py
url(r'^pathology/(?P<pathology_type>[A-Z]{1})/', 'myapp.views.pathology'),
Inside my form, I have a submit with another ajax, so I send an ajax request to another URL
Am I wrong, or is it possible to post to 2 different URL in the same form?
Thanks in advance for your help
I am not sure to understand your problem, but it think your javascript and url.py are inconsistent.
If you keep your javascript, the url should be:
url(r'^pathology/', 'myapp.views.pathology'),
and then get pathology_type from request.POST of your view
If you keep your url, javascript should be:
...
$.ajax({
url:"/pathology/" + pathology_type + "/",
success : function(data){
...

Using jQuery AJAX to Invoke Rails Server Method on Client Side

I am using Ruby on Rails, and I want to invoke a controller method as an onclick event of my pre-existing links.
I am using jQuery and AJAX to do the same. But I don't know much about AJAX.
Here is my code.
<script type="text/javascript">
$("a.browse_history").live("click", function()
{
alert("i am here");
$.ajax({
url: "/user_profile_controller.rb/save_user_history",
type: 'PUT'
});
});
</script>
In user_profile_controller.rb:
def save_user_history
$doc_list << "hello hi"
end
And my link is:
Click here
But this is not working..
Any solutions would be appreciated.
Your URL looks wrong. Run rake routes to get a list of existing routes in your app.
Should be something like
url: "/user_profile/save_user_history"
or
url: "/user_profiles/save_user_history"
Thank you for your reply Stefan.
I changed my code like this:
<script type="text/javascript">
function u_profile(user,url,query)
{
$.ajax({
type: 'POST' ,
url: "http://localhost:3000/user_profiles",
data : {
user_profile : {
user_id : user ,
query_term : query,
visited_url: url
}
} ,
success: function(data){
alert(data);
}
});
};
</script>
And i am invoking it as onclick event of link.
It full fills my requirement of creating an object in database with passed data.

WORDPRESS : Ajax and template

I have a question.
How can I use Ajax on my templates...
in single.php I have :
$.ajax({
type: "POST",
url: "http://www._____wp-content/themes/MS-MangoBerry___/myajax.php",
data: "yo",
cache: false,
success: function(data)
{
alert("yes");
}
});
And in myajax.php, I have
$(document).ready(function() {
alert("ok"); });
Then I have an error : Fatal error: Call to undefined function get_header() in myajax.php
Why ?
Thanks in advance.
Please also have a look at this article http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/#js-global
It suggests that all AJAX requests should be submitted to /wp-admin/admin-ajax.php
And the you could hook the request by using this code in functions.php
add_action('wp_ajax_your_ajax_action_name', 'method_name');
add_action('wp_ajax_nopriv_your_ajax_action_name', 'method_name');
Then you could implement a method in functions.php
function method_name()
{
// do something or echo XML to return stuff
}
On the request you also need to send a parameter name 'action' with value of the action name.
in this case it would be action=your_ajax_action_name.
Hope this help :)
wordpress has a built ajax url that you need to use. this post will help you out.
http://geekpreneur.blogspot.com/2009/06/how-to-use-wpajax-in-wordpress.html
the tricky thing is how wordpress knows which function will accept your call back. it happens by adding an action. the hook of the action is your ajax action prepended with wp_ajax_

Resources