Community
cancel
Showing results for 
Search instead for 
Did you mean: 
José_M_
Beginner
131 Views

Ajax request not working on the emulator

Jump to solution

Hello,

I'm newbie at Intel XDK and this is my first post here, so I'm sorry if my question is out of logic...

I'm starting to build an web app with Intel XDK. For the loggin purpose I've made an Ajax request to a php script.

Questions:

1º - My request is working fine when I go to the published website, however, nothing happens on the emulator. Is that normal? (I've done with jquery ajax call, and with XMLHttpRequest. both gave same result...)

2º - I've seen examples with "AppMobi.device.getRemoteData" and "intel.xdk.device.getRemoteDataWithID". Do this examples only work with cordova?

Thanks in advance...

Tags (2)
0 Kudos
1 Solution
Diego_Calp
Valued Contributor I
131 Views

Hi José,

In the XDK app you keep the same url to call the php login? 

var url = 'php/autentica.php?AUTT='+base64.encode( JSON.stringify(AUTT));

The app is not hosted on your server, could be this the issue?

Regards

Diego

View solution in original post

5 Replies
Diego_Calp
Valued Contributor I
131 Views

Hi José,

Could you post the php ajax request?

Did you check in the  Emulate tab the debug option for errors?

Regards,

Diego 

José_M_
Beginner
131 Views

Hello Diego,

Thanks for your repply.

The request is made like:

var AUTT = { tarefa: 'login',
               login:  document.getElementsByName('auttUsr')[0].value,
               pass:   document.getElementsByName('auttPwd')[0].value,
               lembra: document.getElementsByName('auttRem')[0].checked*1,
               cookie:     (document.cookie.search('sigit')>=0 ? document.cookie.split('sigit=')[1] : false),
             };

var url = 'php/autentica.php?AUTT='+base64.encode( JSON.stringify(AUTT));

$.ajax({type: "GET", 
			url: url,
			beforeSend: function () {},		//   To implement when loading;
			complete: function () {},		//    To implement after loading;
                        success: function(status) {
				var resposta = JSON.parse(status);
				// Functions used on sucess!


			},
			error: function(jqXHR, exception) {
				var mess='';
				if (jqXHR.status === 0) {
					mess="Não se encontra ligado.\n Verifique a ligação de rede.";
				} else if (jqXHR.status == 404) {
					mess="A página requerida não foi encontrada. [404]";                    
				} else if (jqXHR.status == 500) {
					mess="Erro interno de servidor [500].";
				} else if (exception === 'parsererror') {
					mess="O pedido de análise JSON retornou um erro.";
				} else if (exception === 'timeout') {
					mess="Erro de tempo excedido.";
				} else if (exception === 'abort') {
					mess="O pedido Ajax foi abortado.";
				} else {
					mess="Falha a efectuar o login. Detalhes: \n" + jqXHR.responseText;
					}
				var opts={
					message: mess,
					position:"tc",
					delay:2000,
					autoClose:true,
					type:"error"
				};
				$.afui.toast(opts);

			}
		});

 

If I debug, I'll get the error:

Uncaught SyntaxError: Unexpected token < autt.js:53

          $.ajax.success                                              autt.js:53

           j                                                                  jquery.min.js:2

           k.fireWith                                                     jquery.min.js:2

           x                                                                 jquery.min.js:4

          (anonymous function)                                    jquery.min.js:4

 

The autt.js file is where I have the ajax call, and line 53 is where I have "var resposta = JSON.parse(status);".

The jquery version is 2.1.4.

Strangely, this works fine when I use chrome or IE from a computer accessing the server, however it doesn't in the Intel XDK emulator...

One other strange thing is that I inserted an alert, with the var status immediately before line 53.

On the computer with chrome, IE, etc. it gives an alert with the answer.

On the emulator it gives an alert with text of the php file!!! is it possible it is reading the file instead of executing the file?!?

Best regards,

 

José Mendes

 

 

 

 

Diego_Calp
Valued Contributor I
132 Views

Hi José,

In the XDK app you keep the same url to call the php login? 

var url = 'php/autentica.php?AUTT='+base64.encode( JSON.stringify(AUTT));

The app is not hosted on your server, could be this the issue?

Regards

Diego

View solution in original post

José_M_
Beginner
131 Views

OK!

I've solved the problem without understanding it properly:

I didn't put the server in the url.

It works fine in production but not in emulation!...

changing from "php/autentica.php" to "http://server_x/php/autentica.php" will make it work in production and in emulation!

If someone will help me understand why, I would aprecciate...

Thanks in advance,

 

José Mendes

José_M_
Beginner
131 Views

Hi,

 

Sorry, I answered before seeing your answer...

Now I understand!

You are right, the app it's not in the same server.

I feel stupid not to see it immediately...

Thanks for the help.

Best regards,

 

José Mendes

Reply