开发者

jquery response back

I am sending 开发者_StackOverflow社区an ajax call to another page. I want to get a value of a variable, lets call it x, back from that page on success. How can I do that. here is my ajax code

$.ajax({
        type: 'POST',
        url: 'myotherpage.php',
        data: 'loginname=' + loginname ,
        success: function(success) {
            if(success == 1) {

            //get the variable value here           
           } else {

           //do nothing

            }
        }
    });


Your other page should return json, which contains a status variable (1 for success, 0 for fail), and the variable or whatever data you need. Here's an example from a file I have here. It won't run of course, but should give you the idea.

            Req = $.ajax({
                type: 'POST',
                data: this.data.filter,
                url: this.data.DataURL+"listids",
                dataType: 'json',
                timeout: 5000,
                cache: false,
                error: function(){
                    UserNotify({class:'notify_alert', content:'Your request can\'t be completed at this time.<br />An external error has been encountered.  Please wait a moment and try again.'});
                },
                success: function(o){
                    if ( 0==o.status ) {
                        if ( undefined == o.user_msg ) { o.user_msg = '';}
                        UserNotify({class:'notify_alert', content:'Your request can\'t be completed at this time.<br />'+o.user_msg});
                    } else {
                        if ( 0 < o.data.ids.length ) {
                            tli.data.update.ids = o.data.ids;
                        }
                    }
                }
            });


echo out the variable in your php file instead of "1" and have it return null when something is wrong.

$.ajax({
  type: 'POST',
  url: 'myotherpage.php',
  data: 'loginname=' + loginname ,
  success: function(success) {
    if(success == '') {
      // error alert
      alert('Something went wrong. Reload the page and try again.');
     } else {
      alert(success); // alert the value from what you printed out in myotherpage.php
     }
  }
});


I don't know what you are trying to do but to give you an idea,

if(success == 1) {
// codes get executed here if myotherpage.php would display 1
// so I'm wondering how would you create a variable there...
// if you put anything other than just '1' in myotherpage.php, codes inside this "if" will not be excuted

//get the variable value here           
} else {

//do nothing

}

in this,

success: function(data) {...} // "data" is the data being returned from the server
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜