Ajax JSON response with HTTPS protocol

0

Recently I installed a web application and it works correctly, absolutely everything, the problem is that when I install an SSL certificate, and place it in HTTPS protocol, the ajax functions that return a JSON type response give me an error, and I can not find the way to debug it since it only happens when I enter HTTPS mode, the rest works, the other ajax functions that do not return JSON, also work, only those that give me a JSON response.

 <script>                                                
                             jQuery("#search_login").click(                       
                                     function(event){                                 
    var user_login = document.getElementById('login_username');                   
    var user_pwd = document.getElementById('password_username');                 
                                        jQuery.ajax({
                                            type:'POST',
                                            url:'wp-admin/login.php',
                                            data: jQuery("#login-form").serialize(),
                                            success:function(data){                   
                                                  var check = JSON.parse(data);
                                                  var done_account = check.done;       
                                                if(check.user_unknow === "UNKNOW"){
                                              jQuery('#user_wrong').replaceWith('<label id="incorrect_user" style="color:#d82e21">Usuario incorrecto</label>');
                                                }else{
                                                    jQuery('#incorrect_user').replaceWith('<p id="user_wrong"></p>');
                                                }                                                
                                               if(done_account === 'done'){                                                                             
                                                     jQuery('#session_check').replaceWith('<label style="color:#54be73;"><br />Logeado con éxito</label>');
 setTimeout(' window.location.href = "https://www.maseficsoluciones.com/dashboard_validate.php?userData='+check.data+'&id='+check.user_id+'&user='+check.user_name+'&permission='+check.permission+'" ',1000);                         
             }
 if(check.pw_wrong === "FAIL"){
                                                jQuery('#fail_account').replaceWith('<label id="incorrect_pwd" style="color:#d82e21">Contraseña incorrecta por favor intentalo de nuevo.<br /></label>');
                                                }else{
                                                    jQuery('#incorrect_pwd').replaceWith('<p id="fail_account"></p>');
                                                }
                                            },error:function(data){
                                                console.log(data);
                                            },         
                                        });
                                        return false;
                                     });
                                    
                             </script>   

WEB CONSOLE ERROR:

Uncaught SyntaxError: Unexpected token e in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.success (proyecto_home.php:607)
    at i (jquery.js:2)
    at Object.fireWith [as resolveWith] (jquery.js:2)
    at y (jquery.js:4)
    at XMLHttpRequest.c (jquery.js:4)
    
asked by Carlos Estarita 10.03.2017 в 16:58
source

1 answer

2

The problem:

Apparently using HTTP jQuery does not recognize that the response returned by endpoint is a JSON , so it is necessary to execute JSON.parse in success . Instead using HTTPS , jQuery recognizes that the response is a JSON and internally parses, returning in success an object.

Solution:

At the time of making the call ajax , indicate that the response format of endpoint is a JSON , using the property dataType: 'json' .

Example:

//...           
jQuery.ajax({
  type:'POST',
  dataType: 'json', // <-- AQUI indicamos el formato de la respuesta
  url:'wp-admin/login.php',
  data: jQuery("#login-form").serialize(),
  success:function(data){                   
     //var check = JSON.parse(data); <-- AQUI ya no hace falta hacer esto
     var check = data;
     var done_account = check.done; 
     //...
    
answered by 10.03.2017 / 19:11
source