try this
<script>
$(document).ready(function () {
MyFunction();
$("#EnterLongUrl").click(function () {
MyFunction();
});
});
function MyFunction()
{
$.ajax({
type: "POST",
url: "/home/GetShortURL",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: '{ "longUrl" : "' + $("#LongUrlText").val() + '" }',
success: function (results) {
$("#ShortUrl").val(results);
},
error: function (err) {
alert(err.status + " - " + err.statusText);
}
});
}
</script>