成功设置变量值

how can I set the variable dch to be the returned success ajax data?

var aid='11111111V';
var dch = 0;
$.ajax({
    type:"POST",
    url:"new_hpa_fun_aplcval.php",
    data:"aid="+aid,
    success: function(msg) {            
        if (msg =='OK'){
            dch=1;                    
        } else {
            dch=2;
        }
    }
});
if (dch==1){
     //same php code
}else if (dch==2){
     //another php code
}

You can try like this

1) Wrap the ajax within a function and return the value

2) What does "async: false" do in jQuery.ajax()?

function call() {
     var temp = 0;
     $.ajax({
        type:"POST",
        url:"new_hpa_fun_aplcval.php",
        async: false,  // Add this
        data:"aid="+aid,
        success: function(msg) {            
            if (msg =='OK'){
                temp = 1;                    
            } else {
                temp = 2;
            }
        }           
     });
     return temp;
    }        
var dch = call();

Maybe you are unfamiliar with asynchronous operations. In your code, your if-else check at the bottom of your code is actually executing before your success callback.

What you are probably observing is that dch is always 0. You instead need to continue the execution of your code inside the callback:

var aid='11111111V';
var dch = 0;
$.ajax({
    type:"POST",
    url:"new_hpa_fun_aplcval.php",
    data:"aid="+aid,
    success: function(msg) {            
        if (msg =='OK'){
            // perform something here                    
        } else {
            // perform something here
        }
    }
});

In this case, you don't even need the dch variable.

Your other option is to make the AJAX call synchronous by adding async: false to the options in the $.ajax method. This will cause your code to block and wait for the response before it continues executing.

See the Wiki on Ajax for a more technical description of what's happening under the hood of jQuery.