Reputation: 6762
I am unable to check if my code is wrong or there is any syntax error here. I spent more time so thought of asking in stackoverflow.
$(document).ready(function () {
AjaxGet = function (url, storeageLocation, mySuccessCallback) {
var result = $.ajax({
type: "GET",
url: "/Regions/GetView",
async: true,
contentType: 'application/json',
dataType: "html",
success: function (viewData) {
alert(viewData);
storeageLocation = viewData;
mySuccessCallback(viewData);
},
error: function (xhr, ajaxOptions, thrownError) {
}
}).responseText;
return result;
};
alert(result);
});
Upvotes: -1
Views: 100
Reputation: 1523
$(document).ready(function () {
AjaxGet = function (url, mySuccessCallback) {
var result = $.ajax({
type: "GET",
url: "/Regions/GetView",
async: true,
contentType: 'application/json',
dataType: "html",
success: function (viewData) {
mySuccessCallback(viewData);
},
error: function (xhr, ajaxOptions, thrownError) {
}
}).responseText;
return result;
};
// alert(result); // will throw error: undefined variable 'result', because you used this variable inside function definition 'AjaxGet', not in this context
AjaxGet( '/', function(data) { // call the function after defining it to execute and get the results
alert( data );
} );
});
Upvotes: 1
Reputation: 71
Ajax is an async technology. So, you trying to return result before getting the response.
You can turn off async for ajax and that will look like so:
$(document).ready(function () {
AjaxGet = function (url, storeageLocation, mySuccessCallback) {
var result = null;
$.ajax({
type: "GET",
url: "/Regions/GetView",
async: false,
contentType: 'application/json',
dataType: "html",
success: function (viewData, textStatus, jqXHR) {
alert(viewData);
storeageLocation = viewData;
result = textStatus;
mySuccessCallback(viewData);
},
error: function (xhr, textStatus, thrownError) {
result = textStatus;
}
});
return result;
};
alert(result);
});
Upvotes: 1
Reputation: 15860
The only problem here that I think is, that you cannot return a value from an Ajax call.
Try changing the return result
and I think that would work. There isn't any trouble in your code.
Or just try changing the ajax request type to Synchronous. Maybe it would be like
async: false;
Upvotes: 1