samedi 23 avril 2016

Catching oci_execute error with ajax and jquery

I've been looking for a solution for my problem for about 2 days and I didn't find anything.

The thing is that I'm making a website that connects to an oracle database 11g r12 with php and ajax.

What I want is to be able to show an alert whenever an error in oci_execute occurs.

For example:

Here is my jquery code:

$.ajax({
            type: "POST",
            url: "administradorRegistros.php",
            dataType: "json",
            data: par,
            success: function(data) {
                alert("\'"+$("#inputNombre").val()+" "+$('#inputApellido').val()+"\' registrado correctamente.");
                $('form')[0].reset();
            },
            error : function(data) {
                alert("ajax error, json: " + JSON.stringify(data));
                    console.log(data);
            }
        });

Where par is of the form: {key1: 'value1',...,keyn: 'valuen'}

And my php code for detecting the error is something like:

if(!oci_execute($stPersona)){
        $jsonData['error'] = "Error test";
        echo json_encode($jsonData);
    }

So the thing is that I'd like to show an alert to the user depending on the error of the sql procedure, but what I get is the following text in the alert() of the error: section of the jquery code:

ajax error, json: {"readyState":4,"responseText":"
\n\n( ! ) Warning: oci_execute(): ORA-00001: unique constraint (PARQUELIBERTAD.PERSONA_NOID_UK) violated\nORA-06512: at "PARQUELIBERTAD.REGISTROS", line 92\nORA-06512: at line 1 in C:\wamp64\www\WebSite\administradorRegistros.php on line 59\nCall Stack\n#TimeMemoryFunctionLocation\n10.0020320992{main}( )...\administradorRegistros.php:0\n20.0547325552http://www.php.net/function.oci-execute' target='_new'>oci_execute\n( )...\administradorRegistros.php:59\n\n{\"error\":\"Error test\"}{\"error\":\"Error test\"}","status":200,"statusText":"OK"}

You can see that the error is in the response text part, so it will be very difficult to get it. I think there's gotta be an easier way to achieve this.

Any ideas? Technically the question is: How can I do something like the "statusText": "OK" for the message error.

Thanks!




Aucun commentaire:

Enregistrer un commentaire