Hi i have problem with some things
Here is the code
function get_char_val(merk) {
$.ajax({
type: "POST",
url: "char_info2.php",
data: { name: merk },
dataType: "html",
success: function(data){return(data);}
});
}
alert(get_char_val("str"));
when alert comes out it's output the undefined please help fast i'm making this two days xC
get_char_val does not return anything. The success callback needs to have alert(data) in it to return the data from AJAX.
$.ajax is asynchronous - meaning it doesn't happen in order with other code, that is why the callback exists.
Your return; statement will return the value to the anonymous function you pass into the success handler. You cannot return a value like this, you need to invoke another callback instead.
function get_char_val(merk, cb) {
$.ajax({
type: "POST",
url: "char_info2.php",
data: { name: merk },
dataType: "html",
success: function(data){cb.apply(this, data);}
});
}
get_char_val("str", function(data) {
alert(data);
});
You can either set the async:false config parameter in the ajax call options or use the call back.
Using Async:false -
function get_char_val(merk)
{
var returnValue = null;
$.ajax
(
{
type: "POST",
async:false,
url: "char_info2.php",
data: { name: merk },
dataType: "html",
success: function(data){returnValue = data;}
}
);
return returnValue;
}
alert(get_char_val("str"));
P.S: Note that making ajax calls synchronous is not advisable.
Since you do an asynchronous ajax call, the response of the server is handled in the "success" function. The return in your success function is useless, you should use the "data" parameter directly in the function
This should work. Use it like this.
function get_char_val(merk) {
var myReturnData= "";
$.ajax({
type: "POST",
url: "char_info2.php",
data: { name: merk },
dataType: "html",
success: function(data){myReturnData = data;}
});
return myReturnData;
}
alert(get_char_val("str"));
Just understand the problem with your piece of code. Use the return statement under the right function.
Related
I have a simple code-form like this:
$.ajax({
url: "http://myurl/test.php",
type: "POST",
dataType: "html",
data: {
action: "login"
},
success: function (data) {
console.log(data)
}
});
When on PHP server page, I try to print $_POST["action"] yet this field is blank. Why does it work fine if I use $.post(url,data,function)?
$.ajax({
url: "http://myurl/test.php",
type: "POST",
dataType: "html",
data: {
method: "login"
},
success: function (data) {
console.log(data);
}
});
login is a php function
You may use method instead of type.
$.ajax({
url: "http://myurl/test.php",
method: "POST", // <-- Use method, not type
dataType: "html",
data: {
action: "login"
},
success: function (data) {
console.log(data)
}
});
Problem is solved.....
There is a problem with the callback on success event...and yesterday I focused my attention only on the request php in the developers tool....when I used Curl (with the right request) all work correctly and I forgot to check client code in success callback....
Never use post method with developers console,it's very easy to lose more time :=)
here I have jQuery ajax calling a .php file that SOMETIMES executes let's say the following
echo "hello"
Here it is:
$.ajax({
type: "POST",
url: myurl.php
data: data_string,
timeout: 6000,
success: function () {
}
});
I would like to know: is it possible to make the ajax return ERROR and not SUCCESS when something like the previous echo is executed in the PHP file? I mean, checking inside this $.ajax if the php file is executed as I would or not.
EXPLAINING BETTER:
I get error when the request could not be completed and success when it could. But I would like to get like a return value from this PHP file. If it returns 1, I wanna do something. If it returns 2, instead, I wanna do something else. Hope I explained it better..
Thanks in advance.
I recommend using json_encode() within your PHP file. For example:
echo json_encode(array('success' => 'do_foo'));
exit();
Then you can add a conditional within the success callback:
$.ajax({
type: "POST",
url: myurl.php
data: data_string,
dataType: "JSON", //tell jQuery to expect JSON encoded response
timeout: 6000,
success: function (response) {
if (response.success === 'hello'){
console.log(response);
} else {
console.log('else');
}
}
});
Based on your question make php return 1 or return 2. You can make it return 1 on failure and 0 (which is null) on success. Then you can do this for your ajax return.
$.ajax({
type: "POST",
url: "YOUR URL",
data: dataString,
success: function(server_response)
{
if(server_response == 1)
{
alert("You have made a mistake");
return true;
}
HERE YOU WILL PUT WHAT HAPPENS ON SUCCESS
}
});
$.ajax({
type: "POST",
url: myurl.php
data: data_string,
timeout: 6000,
success: function (msg) {
if (msg != "Hi")
{
//TODO
} else
{
//TODO
}
}
});
You can use error callback for this:
$.ajax({
type: "POST",
url: myurl.php
data: data_string,
timeout: 6000,
success: function () {
},
error: function() {
/* Code reacting to error here */
}
});
Also, there are other callback opportunities which you can check out at $.ajax documentation page.
If you are going to "say" fron PHP that there is an error, this can be done with 2 ways:
You can print in PHP a keyword meaning an error and then check it in you success function:
success: function (data) {
if (data == 'error') {
} else {
}
}
Or, the other way, you can provide with PHP right headers to cause "error". Then you can use the error callback as usual. I would choose this way.
I have a piece of jQuery code:
var newData = checkCP(somedata);
if(newData=="hi"){
alert("Welcom");
}
function checkCP(jsData){
$.ajax({
type: "POST",
url: "Process.php",
data: jsData,
dataType: "json",
success: function(data){
if(data.match==1)
return "hi";
else
return "bye";
}
});
}
I don't know why the welcome alert never shows up. I checked everything; the PHP file returns 1, but apparently before it waits on the AJAX response it passes the
if(new=="hi"){
alert("Welcom");
}
Is there any way to wait for the AJAX response, then read the rest of codes in jQuery?
Yes, you can set the 'async' option in $.ajax() to false.
$.ajax({
type: "POST",
async: false,
url: "Process.php",
data: jsData,
dataType: "json",
success: function(data){
if(data.match==1)
return "hi";
else
return "bye";
}
Firstly, please don't use 'new' as a variable name. 'new' already means something.
Now onto the actual question...
when you call checkCP jquery does the ajax post successfully and automatically waits for a response before execuiting the success function. the thing is that the success function is a stand-alone function and returning a value from that does not return anything from checkCP. checkCP is returning null.
As proof try sticking an alert in your success function.
Something like this should do the trick:
function deal_with_checkCP_result(result)
{
if(result=="hi"){
alert("Welcom");
}
}
function checkCP(jsData,callback){
$.ajax({
type: "POST",
url: "Process.php",
data: jsData,
dataType: "json",
success: function(data){
if(data.match==1)
callback( "hi");
else
callback( "bye");
}
});
}
Pass deal_with_checkCP_result as callback
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);
});
i have one AJAX function getting results from php file as bellow
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
success: function(json)
{
g_foo = json.foo;
}
});
now i want to use the value of g_foo but i can't i try using console.log but i am facing strange problem. like
if i use function like that
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
success: function(json)
{
g_foo = json.foo;
console.log(g_foo);
}
});
now i can see the value return from php file
if now i use function like that
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
success: function(json)
{
g_foo = json.foo;
}
});
console.log(g_foo);
now i got error undefined g_foo;
thanks
As ajax is asynchronous, there's no way of making sure that g_foo is available as soon as you call the $.ajax() request, hence it's not available outside that callback.
You could however, specify async:false to that .ajax() call, but it will most likely block the browser, attempting to wait for the request, something I don't think you want to happen.
To use it outside you have two ways:
1)make the call syncronous like this (this is not a best practice as the browser has to wait for the call to continue the flow):
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
async: false,
success: function(json)
{
g_foo = json.foo;
}
});
console.log(g_foo);
2) call a function on success and continue the flow from there using whatever data has been returned from the function (this is the best practice)
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json'
success: function(json)
{
g_foo = json.foo;
yourfunction(g_foo);
}
});
function yourfunction(g_foo){
//here g_foo is set
}