$('#submit_id').click(function(){
function mark() {
$.ajax({
type: "POST",
url: "request1.php",
data: $('#form_id').serialize(),
cache: false,
success: function(data1){
}
});
}
function other() {
$.ajax({
type: "POST",
url: "request2.php",
data: $('#form_id').serialize(),
cache: false,
success: function(data2){
}
});
}
$.when(mark(), other()).done(function(data1, data2)
{
console.log(data1);
$(".results1").html(data1);
console.log(data2); // Result
$(".results2").html(data2);
});
});
I need to pass the returned data into a variable like:
console: undefined
You have to return promise interface from your functions as:
function mark() {
return $.ajax({
type: "POST",
url: "request1.php",
data: $('#form_id').serialize(),
cache: false
});
}
And you don't need to specify success callback here (but you still could).
Your code should work like this:
$.when(mark(), other()).done(function(data1, data2){
console.log(data1);
$(".results1").html(data1);
console.log(data2); // Result
$(".results2").html(data2);
});
function mark() {
return $.ajax({
type: "POST",
url: "request1.php",
data: $('#form_id').serialize(),
cache: false
});
}
function other() {
return $.ajax({
type: "POST",
url: "request2.php",
data: $('#form_id').serialize(),
cache: false
});
}
Try this
$('#submit_id').click(function(){
function mark() {
var res = null;
$.ajax({
type: "POST",
url: "request1.php",
data: $('#form_id').serialize(),
cache: false,
success: function(data1){
res= data1;
}
});
return res;
}
function other() {
var res = null;
$.ajax({
type: "POST",
url: "request2.php",
data: $('#form_id').serialize(),
cache: false,
success: function(data2){
res= data2;
}
});
return res;
}
$.when(mark(), other()).done(function(data1, data2)
{
console.log(data1);
$(".results1").html(data1);
console.log(data2); // Result
$(".results2").html(data2);
});
});
Related
I have an object like this which I retrieve via an AJAX request:
{
"id": "1",
"name": "Eyes"
}
How can I get that data for my text field #id_attributes and #name_attributes? I tried this when a button was clicked, but it gives me undefined/blank?
$.ajax({
url: "to my json",
cache: false,
type: "POST",
data: 'id=' + id,
success: function (result) {
$("#id_attributes").val(result.id);
$("#name_attributes").val(result.name);
}
});
Can anyone help me?
Try:
$.ajax({
url: "to my json",
cache: false,
type: "POST",
dataType : 'json'
data:{id:id},
success: function (result) {
$("#id_attributes").val(result.id);
$("#name_attributes").val(result.name);
}
});
or parse the result
success: function (result) {
var jsonresult = JSON.parse(result);
$("#id_attributes").val(jsonresult.id);
$("#name_attributes").val(jsonresult.name);
}
Add dataType: 'json':
type: "POST",
data: {"id": id},
dataType: 'json',
success: function (result) {
add a dataType option
$.ajax({
url: "to my json",
cache: false,
type: "POST",
dataType : 'json',
data:'id='+id,
success: function (result) {
$("#id_attributes").val(result.id);
$("#name_attributes").val(result.name);
}
});
Hello all I would like to send a request after my first request succeeds
The following is what I am using to make the initial request
$('#registerUser').submit(function (e) {
e.preventDefault();
$.ajax({
type: 'get',
url: 'GM/checkUserExists',
data: $('form').serialize(),
success: function () {
}
});
return false;
});
In order to make the second requewst I am using the following that is not working, it is loading the page but it is loading the page which I don't want. I am sure I am doing this wrong but any help getting me past this would be great.
$('#registerUser').submit(function (e) {
e.preventDefault();
$.ajax({
type: 'get',
url: 'GM/checkUserExists',
data: $('form').serialize(),
success: function () {
type: 'get',
url: 'GM/checkUserExists',
data: $('form').serialize(),
}
});
return false;
});
Any reason why do you get twice checkUserExists ?
$('#registerUser').submit(function (e) {
e.preventDefault();
$.ajax({
type: 'get',
url: 'GM/checkUserExists',
data: $('form').serialize(),
success: function () {
// IF YOU WANT TO CALL A SECOND AJAX REQUEST
$.ajax({
type: 'get',
url: 'GM/checkUserExists',
data: $('form').serialize()
// success: function ....
});
}
});
// return false; // DONT NEED IT IF YOU HAVE e.preventDefault();
});
I am trying to get ajax working with a codeigniter installation.
This is my PHP function:
function test() {
return print_r("hey");
}
This is the JS:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
success: function(data) {
alert(data);
}
});
This works perfectly but, as soon as I add data it doesn't work.
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: {bar:"foo"},
success: function(data) {
alert(data);
}
});
Thanks in advance!
please check the following
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: "&bar=foo&isAjax="+true,
success: function(data) {
alert(data);
}
});
And controller...
function test() {
if($this->input->post('isAjax')){
return print_r("hey");
}
else{
//do another thing
}
}
And another thing if you want to add data in json format then you have to add another property in your $.ajax object that is datatype: "json"
Use following:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
dataType: 'json',
data: {'bar':'foo'},
success: function(data) {
alert(data);
}
});
Or you can use shorthand version:
$.post("http://localhost/code/test", {'bar':'foo'}, function(data) {
alert(data);
});
And your php code should be:
function test() {
echo "hey";
}
Try the following:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: "bar=foo&name=cyberbob",
success: function(data) {
alert(data);
}
});
i have a jQuery code that posts an ID to another php file
<script type="text/javascript">
$(function() {
//More Button
$('.more').live("click",function() {
var ID = $(this).attr("id");
if(ID){
$("#more"+ID).html('<img src="moreajax.gif" />');
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("ol#updates").append(html);
$("#more"+ID).remove();
}
});
}else{
$(".morebox").html('The End');
}
return false;
});
});
</script>
How can I make it post another query along with "last message id"? I have a MySQL query that will output a value (pid). How can I post this along with "last message id"?
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID + "&pid=" + your_pid,
cache: false,
success: function(html){
$("ol#updates").append(html);
$("#more"+ID).remove();
}
});
Here is my solution:
var salji = {
rate: score,
image_id: img_id
};
$.ajax({
type: "POST",
url: "widgets/rate.php",
data: salji,
success: function(msg){
alert('wow');
}
});
I use codeigniter and i want make uniqid code with php as following code(by random_string()):
function coding(){
echo random_string('unique');
}
I want use result top function in a, jquery function (for use in js code), i in jquery code try as this, but this output in alert(coding()) is "undefined". How can fix it?
function coding(){
$.ajax({
type: "POST",
dataType: "text",
url: "coding",
cache: false,
success: function (data) {
return data;
}
})
}
alert(coding()) // in here output is "undefined" !!!?
function coding(){
var temp;
$.ajax({
type: "POST",
dataType: "text",
url: coding,
cache: false,
async: false,
success: function (data) {
temp = data;
}
})
return temp;
}
alert(coding());
You will receive an [object XMLDocument].
It is becuse coding function is asynchronous.
Use follow code:
function coding(){
$.ajax({
type: "POST",
dataType: "text",
url: coding,
cache: false,
success: function (data) {
alert(data);
}
})
}
coding();
function coding(){
$.ajax({
type: "POST",
dataType: "text",
url: "coding",
cache: false,
success: function (data) {
$("#yourDiv").html(data);
}
})
}
You can assign value to another element like div, p etc.
Well, I'm assuming that the coding variable you are passing at the parameter url is a reference to a method in your CI controller.
If that's the case, you just need to wait from the success callback:
$.ajax({
type: "POST",
dataType: "text",
url: coding,
cache: false,
success: function (data) {
alert(data);
}
})
Ok, you should use callback function like this:
function coding(func){
$.ajax({
type: "POST",
dataType: "text",
url: coding,
cache: false,
success: function (data) {
func(data);
}
})
}
coding(function(data)
{
alert(data);
});