Jump to content
  • 0

Call Ajax From Plugin


AllToolKits.com

Question

Hi,

 

I am developing a blesta plugin.

 

I have country drop down, on each country select state drop down should  load corresponding states.

 

Jquery Code:

$( "#country" ).change(function() {
                        var value = $( "#country" ).val();

                            $.post( url + "plugin/name/admin_did/get_states", { country_id: value })
                                .done(function( data ) {
                                alert( "Data Loaded: " + data );
                            });


                            
                    });

Controller

  public function get_states() {
        $c_id = $this->post['country_id'];
        $states = $this->Country->getList($c_id);
        echo json_encode($states);
        exit;

    }

But got error

 

Data Loaded:    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us" dir="ltr">

    <head>

        <title>Blesta</title>

        <link rel="stylesheet" type="text/css" href="/app/views/errors/css/styles.css" />

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    </head>



    <body>

            <div class="program_error">
        <h3>Oh noes!</h3>
        <div class="contents">
            <p>Files does not exist: C:\pathofplugin\views\admin\default\message.pdt on line <strong>120</strong> in <strong>

     

 

What is wrong with this ajax call approch?

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

A few things:

  1. The method "get_states" should be "getStates"
  2. If you're using CSRF tokens (enabled by default), you must pass the CSRF token in the POST request as well, or the request will be denied. I don't see any reason you can't use a GET request instead.
  3. The error is in regards to setting a message via setMessage or flashMessage. If you are calling either of these methods, you should pass additional parameters to these methods that indicate the message view is not from your plugin, but from Blesta core instead.

You might take a look at the admin_clients_add.pdt template for an example of fetching states, and use the blestaRequest function for your request instead.

Link to comment
Share on other sites

  • 0

If you're using CSRF tokens (enabled by default), you must pass the CSRF token in the POST request as well, or the request will be denied. I don't see any reason you can't use a GET request instead.

 

i believe that is the main reason for that error , try using get instead of post .

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...