-2

I am trying to call a URL using AJAX, it is giving 200 OK but the success function is not loading.

Any help would be greatly appreciated.

$(document).ready(function () {
    $("#resulttable tr:even").addClass('grey');
    $("#resulttable tr:odd").addClass('litegrey');

    $(".upload_file").change(function () {
        $("#fakefile").text($(".upload_file").val());
    });

    $("tr").click(function () {
        $(this).after("<tr><td colspan=5><td></tr>");
        $(this).next("tr").find("td").text(loadText());

    });
});

function loadText() {
    $.ajax({
        type: "GET",
        url: "http://www.google.com",
        success: function (dataCheck) {
            alert("hello");
        }
    })
};

Thanks

putvande
  • 15,068
  • 3
  • 34
  • 50
Ramesh Raithatha
  • 544
  • 1
  • 7
  • 18
  • 2
    Are you actually trying to load google via AJAX? Add the error function to AJAX to see what error you get. And probably in your console you will see that you are not allowed to do this. – putvande Nov 17 '13 at 12:47

2 Answers2

0

You don't return anything.

function loadText() {
    $.ajax({
        type: "GET",
        url: "http://www.google.com",
        success: function (dataCheck) {
            alert("hello");
            return dataCheck;
        }
    })
};

Also, you're trying to do a cross-domain ajax request, which doesn't return a 200 status, are you really trying to do something with google?

You need to use JSONP for cross-domain ajax.

0

You can't return a value from a async function like ajax, you need to use a callback pattern to solve this.

$(document).ready(function () {
    $("#resulttable tr:even").addClass('grey');
    $("#resulttable tr:odd").addClass('litegrey');

    $(".upload_file").change(function () {
        $("#fakefile").text($(".upload_file").val());
    });

    $("tr").click(function () {
        $(this).after("<tr><td colspan=5><td></tr>");
        loadText(function (text) {
            $(this).next("tr").find("td").text(text);
        })


    });
});

function loadText(callback) {
    $.ajax({
        type: "GET",
        url: "http://www.google.com",
        success: function (dataCheck) {
            alert("hello");
            callback(dataCheck)
        }
    })
};

Read this question

Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531