1

This is what am trying to achieve, I have a function in controller that generates a random password and returns the same, on button click I want to capture the generated password and display it in the view (password field). Here is the jquery/ajax call:

$(document).ready(function(){
    $("#generate_password").click(function(){
        $.ajax({
        type: "GET",    
        dataType: 'text',
        url: "http://127.0.0.1/public/admin/content/manufacturers/generate_password",                   
        async:false,
        success: function(resp){
            alert(resp); //this alerts BLANK
        },
        error: function(xhr, textStatus, error){
              console.log(xhr.statusText);
              console.log(textStatus);
              console.log(error);
          }
        });     
    });
});

Controller function:

public function generate_password($length = 9)
{
    return $password_str;
}
  1. Is this the correct way to call controller method?

  2. Why am I getting blank value on alert, even though when printed in php am getting correct value?

  3. Coz it's in the admin area can that be the reason, don't think so but is there any interference because of tokens?

3
  • First: doing this over HTTP is obviously insecure. Consider requiring HTTPS. Commented Sep 22, 2015 at 21:03
  • Second: you can try divide-and-conquer to determine whether the client or server is failing. You could send a request manually with something like curl to see what the response is. At first glance, I don't see anything apparent in the ajax call, so that's why I first suggest that you take the client ajax out of the equation. Commented Sep 22, 2015 at 21:08
  • I understand your concern but just trying to get it right first then I'll work on the security aspect Commented Sep 22, 2015 at 21:14

3 Answers 3

1

In most cases, it is going to work okay, however you can use json format(and just json_encode/json_decode will finish the job for you), not sending via GET method and some other modifications. Are you sure that the url is correct, if you open it in your browser, what happens?

Sign up to request clarification or add additional context in comments.

2 Comments

well I tried that too but in that case I get NULL instead on blank value, been trying too many options but the end result seems same, no output. Really wondering if am calling the controller method correctly or there is another method of doing it?
when I open the url directly it shows me blank page
0

You have to echo the variable in your function in place of return. Because return returns the value to the caller whereas echo sends something to the browser. JavaScript will only understand data which is sent to the browser. So, edit your function code as follows:

public function generate_password($length = 9)
{
    echo $password_str;
}

On the other hand you should be doing this over HTTPS rather than HTTP as mentioned by @rodamn

2 Comments

Tried with echo as well but same result, like stated before the url is just for local system, live will be https
I think that your url is not correct. Try enabling url_helper and change your ajax url setting to "<?php echo base_url()?>/Controller_name/Function_name"
0

Well strange but changing the url from:

url: "http://127.0.0.1/public/admin/content/manufacturers/generate_password",

to this:

url: "generate_password",

did the trick and everything is working fine now :)

Thanks everyone for their valuable inputs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.