Yii Framework Forum: Failed to send Javascript data to action by POST method... - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Failed to send Javascript data to action by POST method...

#1 User is offline   Fabricio Castro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 08-May 17

Posted 19 June 2017 - 12:18 AM

I have a problem, I make the connection to my action from the Javascript through AJAX and send the data by POST. I declare the URL "/ controller / action" and it does not connect to the action, and AJAX gives me "parsererror". The strange thing is that I did one the same but to receive of an equal action and it did not give me error and I worked well

My JS File
$.ajax({
        url: '/user/save-account',
        type: 'POST',
        dataType: 'json',
        data: {
            'id_user': xxx,
            'email': xxx,
            'name': xxx,
            'type': xxx
        },
        contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-15',
        async: false,
        success: function (response) {
        },
        error : function (request, error) {
            console.log(error);
        }
    });



My action in controller
public function actionSaveAccount(){
        $model= new \frontend\models\ProfileForm();
        if (Yii::$app->request->isAjax) {
            if ($model->load(Yii::$app->request->post())){
                if($profile === $model->saveProfile()){
                    // SAVED
                }else{
                    // ERROR SAVED
                }
            }
        }
    }


My settings for URLs
'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            ],
        ],

0

#2 User is offline   Roberto Braga 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 166
  • Joined: 18-February 13
  • Location:Roma, Italy

Posted 19 June 2017 - 01:21 AM

I think the problem is

if($profile === $model->saveProfile()){


You didn't set $profile so yii respond with a notice error which cause the ajax error on client side.

When dealing with ajax is useful to use tool liker the developers tools built-in in chrome and firefox or extension like firebug so you can easily analyze the ajax communication.
0

#3 User is offline   alrazi 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,289
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 19 June 2017 - 01:23 AM

you are getting parse error because you are probably not returning json, double check if your response is json encoded.
0

#4 User is offline   Fabricio Castro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 08-May 17

Posted 19 June 2017 - 02:00 AM

View Postalrazi, on 19 June 2017 - 01:23 AM, said:

you are getting parse error because you are probably not returning json, double check if your response is json encoded.


Yes is JSON encoded
0

#5 User is offline   Fabricio Castro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 08-May 17

Posted 19 June 2017 - 02:02 AM

View PostRoberto Braga, on 19 June 2017 - 01:21 AM, said:

I think the problem is

if($profile === $model->saveProfile()){


You didn't set $profile so yii respond with a notice error which cause the ajax error on client side.

When dealing with ajax is useful to use tool liker the developers tools built-in in chrome and firefox or extension like firebug so you can easily analyze the ajax communication.


The problem is that it does not perform anything in the action, but when the redirect does not execute the action
0

#6 User is offline   Roberto Braga 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 166
  • Joined: 18-February 13
  • Location:Roma, Italy

Posted 20 June 2017 - 07:10 AM

View PostFabricio Castro, on 19 June 2017 - 02:02 AM, said:

The problem is that it does not perform anything in the action, but when the redirect does not execute the action

What do you mean exactly?
If you put a
die('hello');
as first row of the action do you see it as answer to you ajax?
if you disable the ajax check in the action and call the url directly from the browser do you get anything?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users