Understanding ajax POST method, finding url - ajax

I don't fully understand one thing, how the next code gets the url
parameter. I would be glad if someone could help.
$('.nav-item.home').addClass('active');
$('#searchForm').submit(function(event){
$(this).find('input[name=csrf]').val($('meta[name=csrf]').attr('content'));
$.ajax({
beforeSend: function(){
$('.spinner').show();
$('.innerText').html('');
},
type: 'POST',
data: $('#searchForm').serialize(),
url: $('#searchForm').data('action'),

The URL parameter for the AJAX call is brought in from the <form> tag:
url: $('#searchForm').data('action')
On the page there is:
<form id="searchform" action="urltosomewhere">
Is that what you are looking for?

Related

Meteor HTTP.call and jquery ajax

I would like to use Meteor.call('GET') instead of $.ajax(). I have an ajax call as the following:
$.ajax({
url: url,
crossDomain:true,
type: method,
data: query,
dataType: 'json'
}).done(function(data) {
_tokens.request = {
token: data.oauth_token,
secret: data.oauth_token_secret.split('')
};
});
Have some options but I don't know how to pass to Meteor.call(). Please help!
Thank you so much
You probably mean HTTP.call or HTTP.get. Meteor.call is related to another concept.
http://docs.meteor.com/#/full/http_get

redirecting is not working in ajax

This below script is in view/template folder.
<script>
$(document).ready(function(){
$("#change").click(function(){
$.ajax({
type: "POST",
url: "common/customer.php",
data: { oldemail: $("#oldemail").val(), newemail: $("#newemail").val() }
});
});
});
</script>
I want to send data in common/customer.php but it is not working. I
already used ../common/customer.php but same problem. What is the
solution?
Its a absolute path problem.
Your script is in view/template
If your path of javascript file is like something,
view/template/myjavascript.js
And if your path of customer.php file is
view/common/
Then you must switch your directory by ../
Use
../common/customer.php
Its a relative path of your file.
According to jQuery documentation, you must declare the data type:
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType
});
TRY...
url: "/common/customer.php", // note the leading slash
Sorry friends problem was in my html code. I used the type of input submit but when i changed it into button then it is working fine. Thanks to all of you.

Needs tips on AJAX

Im going to start AJAX can someone help, guide me to how should i do it ,where should i learn it from.
(I have membership in Lynda they have a course on AJAX [only 2 hours long] i don't think that would be enough. right?).
jQuery AJAX documentation: http://api.jquery.com/jquery.ajax/
I recommend this method:
$.ajax({
type: "POST",
url: 'ajax_controller.php', //url to u controller in PHP
data: { //Data transmitted to the controller
id: id,
action: 'delete'
}
}).done(function(result){
console.log(result);//show controller result in console
});

Linking AJAX with PHP code. Do i need to write it again?

I have a real problem. I have a php code and a form where when the form is submitted a POST request is sent and the page reloads again and ofcourse the post is viwed in the page.But i want to work with AJAX in order page not to be refreshed.I know the basics of AJAX but i don't want to build all the project from the beggining.Is there a way in success function of AJAX to link to my php code??
$.ajax({
type: "POST",
url: "index.php",
datatype: "html",
data: dataString,
success: function(data) {
//How can i link here to start running my php code which is located in the same page.
}
});
$.ajax({
type: "POST",
url: "somescript.php",
datatype: "html",
data: dataString,
success: function(data) {
// try this
console.log(data);
// see what 'data' actually is
}
});
then check in the browser by hitting F12 to look at the console.
also are you sure you want datatype of html ? you probably want a data type of json or XML , what is the server returning to you after the ajax post?
You have to cancel the form's submission so that the ajax request will take place, otherwise it is canceled. Also use .serialize to get a name-value pair string of the form data to use in the ajax call.
Html
<form id="MyForm">
<button id="MyButtonId">Submit</button>
</form>
JS
$("#MyForm").submit(function(e){
//Prevents the form from being submitted
e.preventDefault();
$.ajax({
type: "POST",
data: $("#MyForm").serialize(),
url: "somescript.php",
datatype: "html",
data: dataString,
success: function(data) {
alert(data);
}
});
});

Symfony2 Ajax app_dev.php in url

Just starting out in Symfony2 and really loving it after being a long time ZF1 developer.
Started to add some Ajax functionality to a site tonight and am a bit confused about the following.
in my ajax call eg:
$.ajax({
url: '/app_dev.php/ajax/urlgetter',
data: "url="+urlinput,
dataType: 'html',
timeout: 5000,
success: function(data, status){
// DO Stuff here
}
});
I had to add /app_dev.php to the url to make it work in dev environment. Is there not a better way of doing this? Does this mean when I change the project to a production environment I need to search and replace all instances of /app_dev.php?? Hopefully I have totally missed something simple.
I ended up using the jsrouting-bundle
Once installed I could simply do the following:
$.ajax({
url: Routing.generate('urlgetter'),
data: "url="+urlinput,
dataType: 'html',
timeout: 5000,
success: function(data, status){
// DO Stuff here
}
});
where urlgetter is a route defined in routing.yml like:
urlgetter:
pattern: /ajax/urlgetter
defaults: { _controller: MyAjaxBundle:SomeController:urlgetter }
options:
expose: true
notice the expose: true option has to be set for the route to work with jsrouting-bundle
I guess this question is already kind of old but I came across the same problem.
Its not any "best practice" solution but here is what I do.
In twig you can use this {{ path('yourRouteName') }} thing in a perfect way. So in my twig file I have a structure like that:
...
<a href="{{ path('myRoute') }}"> //results in e.g http://localhost/app_dev.php/myRoute
<div id="clickMe">Click</div>
</a>
Now if someone clicks the div, I do the following in my .js file:
$('#clickMe').on('click', function(event) {
event.preventDefault(); //prevents the default a href behaviour
var url = $(this).closest('a').attr('href');
$.ajax({
url: url,
method: 'POST',
success: function(data) {
console.log('GENIUS!');
}
});
});
I know that this is not a solution for every situation where you want to trigger an Ajax request but I just leave this here and perhaps somebody thinks it's useful :)
Since this jQuery ajax function is placed on twig side and the url points to your application you can insert routing path
$.ajax({
url: '{{ path("your_ajax_routing") }}',
data: "url="+urlinput,
dataType: 'html',
timeout: 5000,
success: function(data, status){
// DO Stuff here
}
});

Resources