1
  function regist(nome,pwd){
        URL+="register?nick=" + nome + "&key=" + pwd;
        var req = new XMLHttpRequest();
        req.open("get", URL, true);//Assincrono

        req.onreadystatechange = function(){
            if (req.readyState != 4) { 
                return;
            }
            else if (req.status != 200) {// Tratamento de erro (e.g., mostrar mensagem de erro)
                return;
            }
            var data=JSON.parse(req.responseText);
                    alert(data);
            if(data.error==null || data.error==undefined){
                    document.getElementById("register").innerHTML="Registo Bem Sucedido";
                }
                else{
                    document.getElementById("register").innerHTML="Utilizador já registado - Password errada";
                }
        };
        req.send();
    };

I CANT ACCESS at var data.. Maybe because "*req.onreadystatechange = function(){-----" What's appening? My alert to data isn't working, it was not readed.. Can you help me?>the URL is correctly defined, don't worry ;)


I don't know nothing about same origin policy problem... The URL used in GET is: dcc.fc.up.pt:8080/TabuWeb/rest/register?nick=ola&key=mundo My page URL: file:///home/carlos/public_html/TabuWeb2/WebContent/index.html?nick=ola&key=mund‌​o

Is there any problem about same origin policy??That's Why my req.status=0 and firebug is pointig to req.send()?

Thanks

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
cpfp
  • 377
  • 1
  • 4
  • 15
  • What content does `alert(data)` show? Or does it throw an exception before it gets there? What, specifically, happens when this code is run? – Alex Wayne Nov 20 '12 at 02:17
  • Use `return console.log(req.readyState, req.status);` where your `return;`s are and instead of the `alert`, and you'll be able to follow every step of the request. (You may find it never reaches 200 OK). You also say `URL` is correctly defined but I'll point out that [`window.URL`](https://developer.mozilla.org/en-US/docs/DOM/window.URL) exists so if you're `+=`ing it, you'll end up with weird results. – Paul S. Nov 20 '12 at 02:19
  • Well, I got 0 after doing an alert(req.status): So, why req.status=0? I discovered other thing, i started a console.log(req), and I got "GET:"URL" (LOADING)" pointing to server.js(line 52) (req.send(null)). If I do "resend", the response is shown by FIREBUG.. Well, I get this after doing an alert(req.status): else if (req.status != 200){ alert(req.status) ------>>>>>0 return;} So, why req.status=0? I discovered other thing, i started a console.log(req), and I got "GET:"URL" (LOADING)" pointing to server.js(line 52) (req.send(null)). If I do "resend", the response is shown by FIREBUG.. – cpfp Nov 20 '12 at 03:36
  • You're not trying to do cross-domain and therefore running into the same origin policy, are you? – Paul S. Nov 20 '12 at 10:18
  • I don't know nothing about same origin problem... the URL used in GET is: http://www.dcc.fc.up.pt:8080/TabuWeb/rest/register?nick=ola&key=mundo My page URL is: file:///home/carlos/public_html/TabuWeb2/WebContent/index.html?nick=ola&key=mundo Is there any problem abou same origin policy??Thanks – cpfp Nov 20 '12 at 12:21
  • This is the [same origin policy](https://developer.mozilla.org/en-US/docs/Same_origin_policy_for_JavaScript). `http://dcc.fc.up.pt` is a different domain name and protocol to `file://`, which [has it's own special case scenarios](https://developer.mozilla.org/en-US/docs/Same-origin_policy_for_file:_URIs) although these can vary cross-browser. Put simply, unless you change your browser's security settings this request will always fail. – Paul S. Nov 20 '12 at 16:49

1 Answers1

0

There could be a number of reasons why it's not working, and it's hard to tell just from the ajax call.

First, to see your data you should use:

alert(JSON.stringify(data));

Or directly, to check if ajax returns valid json:

alert(req.responseText);

data is an object, so alert(data) won't show much.

Also, is your url in the same domain as your current page? If not, you might be victim of the same origin policy.

Christophe
  • 27,383
  • 28
  • 97
  • 140
  • Well, I got 0 after doing an alert(req.status): So, why req.status=0? I discovered other thing, i started a console.log(req), and I got "GET:"URL" (LOADING)" pointing to server.js(line 52) (req.send(null)). If I do "resend", the response is shown by FIREBUG.. Well, I get this after doing an alert(req.status): else if (req.status != 200){ alert(req.status) ------>>>>>0 return;} So, why req.status=0? I discovered other thing, i started a console.log(req), and I got "GET:"URL" (LOADING)" pointing to server.js(line 52) (req.send(null)). If I do "resend", the response is shown by FIREBUG.. – cpfp Nov 20 '12 at 03:35
  • I don't know nothing about same origin problem... the URL used in GET is: http://www.dcc.fc.up.pt:8080/TabuWeb/rest/register?nick=ola&key=mundo My page URL is: file:///home/carlos/public_html/TabuWeb2/WebContent/index.html?nick=ola&key=mundo Is there any problem abou same origin policy??Thanks – cpfp Nov 20 '12 at 12:21
  • @CarlosPiedade The same origin policy states that you can only get dcc.fc.up.pt:8080 from a page that belongs to dcc.fc.up.pt:8080. Then there are exceptions, but in your case this is the issue. This is a wide subject, difficult to summarize in a forum reply, and I encourage you to search the Web to learn more about it and the workarounds (trusted server, CORS, JSONP). – Christophe Nov 20 '12 at 16:38
  • thanks, I'll talk with my teacher to see if he knows what is wrong. It's hard to solve this . – cpfp Nov 20 '12 at 16:49