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);
});
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);
}
});
I want to retrieve parameter passes from ajax in Controller in Codeigniter but did not know how to use it my ajax function is below
function sendVideoData(frm_id)
{
var data = new FormData(document.getElementById("post_video_"+frm_id));
// make the AJAX request
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>"+"dashboard/do_upload",
data: data+'&form_id='+frm_id,
mimeType:"multipart/form-data",
contentType: false,
cache: false,
processData:false,
dataType: 'json',
success: function (data) {
alert(data);
},
});
return false;
}
</script>
I want to retrieve
form_id
in my controller and use it
It's pretty simple,
echo $this->input->post('form_id')
or simple php
echo $_POST['form_id']
inside your controller dashboard and method do_upload
You can get all list of post variables using print_r($this->input->post()) as well.
Your AJAX request can be:
jQuery.ajax({
type: "POST",
url: "<?php echo base_url() ?>dashboard/do_upload",
data: 'form_id=' + frm_id + '&data=' + data,
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData:false,
dataType: 'json',
success: function (data) {
alert(data);
},
});
And in ur controller: $this->input->post('form_id');
I am noob at using jquery and ajax. I need to change the form from $.post to $.ajax .
var disqus_config = function() {
this.callbacks.onNewComment = [function(comment) {
$.post("sendnotification", { comment: comment.id, post: $post->id,author:$author->id}, function(result){
alert(result);
});
}];
};
I know I need to end something like here but I am stuck how to use post datas(comment,post,author) inside this function
$.ajax({
url: 'sendnotification',
type: 'POST',
data: 'query=' + query ,
dataType: 'JSON',
async: true,
success: function(data){
process(data)
}
Thanks
Just use the same object literal you did for $.post, eg (gotta assume that's some PHP or something in there)
$.ajax({
url: 'sendnotification',
type: 'POST',
data: { comment: comment.id, post: {$post->id}, author: {$author->id} },
dataType: 'json',
async: true,
success: function(data){
process(data)
}
});
I believe dataType: 'JSON' should be changed to dataType: 'json'
Also, use the same data array as you used in your $.post variant.
$.ajax({
url: 'sendnotification',
type: 'POST',
data: { comment: comment.id, post: $post->id,author:$author->id } ,
dataType: 'json',
async: true,
success: function(data){
process(data)
}
});
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 am trying to perform a nested AJAX call using the following code. The nested call doesn't seem to work. Am I doing anything wrong?
$.ajax({
type: 'GET',
url: "/public/customcontroller/dosomething",
cache: false,
dataType: "html",
success: function(html_input)
{
$.ajax({
type: 'GET',
url: "/public/customcontroller/getjobstatus",
cache: false,
dataType: "html",
success: function(html_input){
alert(html_input);
}
});
}
});
You could be having thread-type issues, given that the outside call is asynchronous and may go out of scope before the success handler can come into play. Try breaking the success call out into a separate function.
Try this
$.ajax({
type: 'GET',
url: "/public/customcontroller/dosomething",
cache: false,
dataType: "html",
async: false,
success: function(html_input)
{
$.ajax({
type: 'GET',
url: "/public/customcontroller/getjobstatus",
cache: false,
dataType: "html",
success: function(html_input){
alert(html_input);
}
});
}
});
Use the async/await syntax.
async function foo () {
var html_input = await $.ajax({
type: 'GET',
url: "/public/customcontroller/dosomething",
cache: false,
dataType: "html"
})
var html_input2 = await $.ajax({
type: 'GET',
url: "/public/customcontroller/getjobstatus",
cache: false,
dataType: "html"
})
alert(html_input2)
}
foo().catch(e => console.log('some error:', e))