Problems accessing PHP page via Ajax

Asked

Viewed 124 times

2

I am having trouble accessing a PHP page via ajax. When I am trying to access the access.log the following error:

"POST /Eglise/mobile/Mob_acao.php HTTP/1.1" 200 84 "-" "Mozilla/5.0 (Linux; Android 4.4.2; XT920 Build/3_190_2009) Applewebkit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0 Mobile Safari/537.36"

In ajax I’m doing so:

 jQuery.ajax({
        type: "POST",
        url: w_servidor+"/MOB_Acao.php",
        data: dados,
        success: function( data )
        {
            var e = $.parseJSON(data);
            console.log(e.codError == 506);
        }
 });

My w_server variable is var w_servidor = "http://meuip:8080/eglise/mobile";

I looked about htaccess but it has nothing to do with what is happening. The project is mobile, and what makes us more interesting is that in mobile works perfectly.

This request specifically I am making a query, the page that receives the request is written like this:

<?php

include "MOB_PessoasBanco_class.php";
$acao = $_POST['acao'];
error_log($acao);
if ($acao == 'verificar') {
    $actionExecute = new PessoasBanco($_POST, 'verificar');
    echo $actionExecute->verificarPessoas();
}

if ($acao == 'cadastrar') {
    $actionExecute = new PessoasBanco($_POST, 'cadastrar');
    echo $actionExecute->cadastrarPessoa();
}

The page I call next does the search in the database as follows:

$data = $this->conexao->fetchNaoRestritivo($sql, $dataInput);

But the problem is not in this part, because when I open the IntelXDK it works perfectly, and I even did another project to test this connection and everything worked. So the problem is in my code, or in another part of the project.

  • your access.log message is returning ok, what kind of data Voce is sending to the server ?

  • I’m sending code, email, and password

  • to access my database and take a test.

  • In fact, he’s not even accessing my page.

  • What I’m seeing Oce wants to send data from a form via post with ajax how and that Oce is recovering this data Oce is entering or consulting in database? more details of the problem

  • 4

    the use of parse is used in $.post(), for $.ajax({ }) , you must inform the type: dataType:'json', and what is the structure of: data ?

  • At first I saw no problem in terms of code. What it looks like is, you’re making the request correctly, but you’re not managing to handle the return. In PHP code try to return an array or json and in the Ajax request datatype set the return data type.

Show 2 more comments

1 answer

1


This information in the access log says that the request returned status 200 (Ok). It is not an error. The problem, as Ivan said, is that with $.ajax() you should not use $.parseJSON(). You need to specify the return type in the parameter dataType ajax.

Would look like this:

 $.ajax({
            type: "post",
            url: w_servidor+"/MOB_Acao.php",
            data: dados,
            dataType: "json",
            success: function( data )
            {
                // Data é o Array correspondente ao JSON retornado pelo webservice.

                //Você pode dar um console.log(data) para ver a estrutura do Array
            }
     });

Ah, if it is not, monitor the sending of the request in the console, see if it will return the status 200 even. If it does not return, comments here the code returned.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.