获取ajax发布成功数据

I have the following ajax call. What I would like to do is set the variable "lan_setting" during the ajax requests, and be able to use that variable on success.

In reality I want to set that variable to be post data, which will vary depending on the form input, but as of now I can't even get it working with just this basic example. It just returns "undefined".

_jqXHR = $.ajax({
    url: url,
    data: {lan_setting: 'en'},
    scriptCharset: "UTF-8",
    contentType: "application/x-www-form-urlencoded;charset=UTF-8",
    success: function(lan_setting, data, textStatus, jqXHR) {
        alert(data.lan_setting);    
    }
});

How do I can I use post variable sent via ajax on success?

thanks!

Well, if you're posting, you should use the jquery post function here

$.post(
    url,
    {lan_setting:"en"},
    function( data, status, jqXhr ){
        alert(data.lan_setting);
    },
    "json"
);

then php:

<?php
    // do stuff

    $response = new stdClass;
    $response->lan_setting = $_POST["lan_setting"];
    print json_encode($response);
?>

Well the success() method in jQuery.ajax, accepts 3 parameters.. The first being the response from the request.

success(data, textStatus, jqXHR)Function, Array A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter; a string describing the status; and the jqXHR

Also, when using $.ajax, there are a certain number of objects you can pass. See http://api.jquery.com/jQuery.ajax/

As far as your post, you are able to do...

$.post("service.php", {lan_setting: "en"}, function(response) { alert(response); }

which will post the second parameter, {lan_setting: "en"} to that php service, and echo it's response.

Well, you're declaring the success function wrong (from the jQuery .ajax() documentation):

success(data, textStatus, jqXHR)

In other words, the success function gets data, textStatus, jqXHR and nothing else. You can't just will it to take your POST variable -- it only gets what it gets. You also can't pass a POST variable in by just specifying it in the config object: you have to pass it in via the data property. Lastly, .ajax() defaults to a GET request, so you have to explicitly specify that you want to use a POST request.

I'm a little confused about what you want to do; if you know the value of lan_setting before making the AJAX call, why do you need to have it passed into the success function? Just use it:

var lan_setting = 'en';
_jqXHR = $.ajax({
    url: url,
    type: "POST",
    data: {
        lan_setting: lan_setting
    },
    scriptCharset: "UTF-8",
    contentType: "application/x-www-form-urlencoded;charset=UTF-8",
    success: function(lan_setting, data, textStatus, jqXHR) {
        alert(lan_setting); 
    }
});

If, on the other hand, you want to pass the lan_setting value in, have it modified by the server, and passed back, you will have to somehow encode it in the response, probably with JSON.