The main (but not only) reason your code doesn't work is because the ajax call is asynchronous, that effectively means it happens in the background, or in parallel with the rest of the code. So code execution doesn't stop at the ajax call and wait for a response, so you call ajax which goes off and does its thing, your code then advances to the alert but the ajax call hasn't finished yet.
You can make ajax wait for a response before continuing by adding "async:false" as an option to the call, but that's generally not needed. To store the result you'd do something like
var myResponse;
$.ajax({
url: 'PageMethod/GetData',
method: 'post',
dataType: 'json',
data: JSON.stringify({ dataId: "xxx" }),
contentType: 'application/json',
success: function (data) {
myResponse = data.d.responseText;
},
error: function (ex) {
alert(ex.responseText);
}
});
myResponse is now a variable you can use anywhere, but as I said you have to understand the nature of asynchronous calls and what they're doing, that variable is only usable *after* the success has been called so you need to bear that in mind when it comes to using the variable. If you have a process that relies on this data you usually do something in the success event to kick that process off as you know at that point the variable has data.