0

In my project i create an object when an input text blur like this:

inputT1.onblur = function(){
            var pDomain = this.value;
            if (pDomain.substring(0,4) === "http") {
                var getLocation = function(href) {
                    var l = document.createElement("a");
                    l.href = href;
                    return l;
                    };
                var l = getLocation(pDomain);
                pDomain = l.hostname;

            }

            responseT = $.get( "neuroscan/php/trial500.php", { P1: 'dominio', P2: pDomain } );
            console.log("Status: "+responseT.responseText);

            }

Console respond Status: undefined and if i write:

console.log("Status: "+JSON.parse(responseT.responseText));

return an error. The object responseT was create (as you can see from the picture) and responseText have a value, but I can't isolate it into a variable

enter image description here

Any idea?

Thanks in advance

AleMal
  • 1,977
  • 6
  • 24
  • 49
  • 1
    possible duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – JJJ Nov 07 '14 at 09:58
  • I read that post but my problem is that despite the existence of the object responseT if I write responseT.responseText console me back "undefined" – AleMal Nov 07 '14 at 10:02
  • responseText filled when request become complected. So when you call console.log at first time - it is undefined. But it filled later – Vasiliy vvscode Vanchuk Nov 07 '14 at 10:04
  • Your problem is that you're testing them at different times. When you type "responseT" in the console it has already been resolved. But when you have `responseT.responseText` in the code, it hasn't been resolved yet and you're accessing promise. The duplicate will solve your problem. – JJJ Nov 07 '14 at 10:05

1 Answers1

1

$.get return promise, not data. Thus it work ansyncroniusly

inputT1.onblur = function(){
        var pDomain = this.value;
        if (pDomain.substring(0,4) === "http") {
            var getLocation = function(href) {
                var l = document.createElement("a");
                l.href = href;
                return l;
                };
            var l = getLocation(pDomain);
            pDomain = l.hostname;

        }

         $.get( "neuroscan/php/trial500.php", { P1: 'dominio', P2: pDomain }, function(data, status, xhr){
                 console.log(data);
               });
        }
Vasiliy vvscode Vanchuk
  • 7,007
  • 2
  • 21
  • 44