cancel
Showing results for 
Search instead for 
Did you mean: 

get error 403 in Ajax within webscript

cperez
Champ in-the-making
Champ in-the-making
Hi all.
I'm using a script within my ftl file and when the url is ok all works fine, but when the page response is 403 I can't show the alert message insede the if.

This is the code:

$.get(url, function(data,status){
            if (status.code=="403"){
               alert ("Something is wrong");
            }
            ticket = data.toString().substring(48,95);
            window.location.href = "${url.serviceContext}/showContent?name=${docName};
         },'html'
         );



What is the mistake??

Tanks in advance!!
3 REPLIES 3

kaynezhang
World-Class Innovator
World-Class Innovator
Did you login to alfresco befor you call the url? Error 403 means "authentication error".
You should first login to alfresco and get a ticket and append the ticket as parameter to your url.

cperez
Champ in-the-making
Champ in-the-making
Hi kaynezhang
No, because I want to catch the 403 error when I call the http://localhost:8080/alfresco/service/api/login?u=user&pw=pass with a icorrect user/password.
When the user is correct the script works fine and I get the funcionality, but when the user is incorrect I want to show a specific message error (now I only show a white page because I don't have anything defined in body section), this is my code within "getContent.get.html.ftl":


<html>
   <head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
      <script type="text/javascript">
         jQuery.support.cors = true;
         var urlTicket = "${url.serviceContext}/api/login?u=${user}&pw=${pass}";
         var ticket = "";
         $.get(urlTicket, function(data,status){
            if (status.code=="403"){
               status.code = 403;
               status.message = "File not found.";
               status.redirect = true;
            }
            ticket = data.toString().substring(48,95);
            window.location.href = "${url.serviceContext}/showContent?name=${docName}&alf_ticket="+ticket;
         },'html'
         );
      </script>
      <title>Show file</title>
   </head>
   <body>
   </body>
</html>


The $.get… works and I get the user ticket only if the user and the password are correct. Otherwise I can't call the file "getContent.get.html.403.ftl" to show my own message. I want to show that message but I don't know how.

What am I doing wrong?? or Are there other way to do that??

Thanks a lot in advance!!

cperez
Champ in-the-making
Champ in-the-making
Hi.
I find one problem in my code and I solved it (The error code must be run after the $.get…).


<html>
   <head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
      <script type="text/javascript">
         jQuery.support.cors = true;
         var urlTicket = "${url.serviceContext}/api/login?u=${user}&pw=${pass}";
         var ticket = "";
         var request = $.get(urlTicket, function(data,status){
            ticket = data.toString().substring(48,95);
            window.location.href = "${url.serviceContext}/showContent?name=${docName}&alf_ticket="+ticket;
         },'html'
         );
         request.error(function(jqXHR, textStatus) {
            if (textStatus == 'error'){
               alert("Error: " + status.code);
               status.code = 403;
               status.message = "File not found.";
               status.redirect = true;
               alert("Error: " + status.code);
            }
         });
        
      </script>
      <title>Show file</title>
   </head>
   <body>
   </body>
</html>


But now I have another issue. In the request.error(…){…}, I see the 2 alert messages within the if statement with this message:
          "Error: undefined"

I think that the problem is with these 3 lines of code, but I don't know why because I use this statement in other file which works well.

status.code = 403;
status.message = "File not found.";
status.redirect = true;



Thanks a lot in advance!!