A
A
Alexander Ulo2016-05-11 20:42:22
PHP
Alexander Ulo, 2016-05-11 20:42:22

Opencart + ajax = Forbidden 403?

I add the function of sending a message to the opencart.

Processing request:

$('.button-modal').live('click', function() {
  $.ajax({
    url: 'index.php?route=product/product/modal',
    type: 'post',
    data: $('.modal-form textarea[type=\'text\'], .modal-form input[type=\'text\'], .modal-form input[type=\'hidden\']'),
    dataType: 'json',
    beforeSend: function() {
      $('.button-modal').after('<span class="wait">&nbsp;<img src="catalog/view/theme/default/image/loading.gif" alt="" /></span>');
    },	
    complete: function() {
      $('.wait').remove();
    },			
    success: function(json) {
      $('.warning, .error').remove();
            
      if (json['error']) {
        if (json['error']['name']) {
          $('.modal-form input[name=\'name\']').after('<span class="error">' + json['error']['name'] + '</span>');
        }

        if (json['error']['email']) {
          $('.modal-form input[name=\'email\']').after('<span class="error">' + json['error']['email'] + '</span>');
        }
        
        if (json['error']['phone']) {
          $('.modal-form input[name=\'phone\']').after('<span class="error">' + json['error']['phone'] + '</span>');
        }
        
        if (json['error']['message']) {
          $('.modal-form textarea[type=\'text\']').after('<span class="error">' + json['error']['message'] + '</span>');
        }
                                                                        
      } else {
        $('.modal-form').html('Your message has been sent');
      }
      
    },
    error: function(xhr, ajaxOptions, thrownError) {
      alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    }
  });	
});

php entry in product.php:
public function modal() {

    $json = array();

    if (!$json) {					
      if ((utf8_strlen($this->request->post['name']) < 1) || (utf8_strlen($this->request->post['name']) > 32)) {
        $json['error']['name'] = 'error name';
      }

      if ((utf8_strlen($this->request->post['email']) > 96) || !preg_match('/^[^\@][email protected]*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
        $json['error']['email'] = 'error email';
      }
      
      if ((utf8_strlen($this->request->post['phone']) < 1) || (utf8_strlen($this->request->post['phone']) > 32)) {
        $json['error']['phone'] = 'error phone';
      }
      
      if ((utf8_strlen($this->request->post['message']) < 1) || (utf8_strlen($this->request->post['message']) > 32)) {
        $json['error']['message'] = 'error message';
      }
    }

    if (!$json) {
      $mail = new Mail();
      $mail->protocol = $this->config->get('config_mail_protocol');
      $mail->parameter = $this->config->get('config_mail_parameter');
      $mail->hostname = $this->config->get('config_smtp_host');
      $mail->username = $this->config->get('config_smtp_username');
      $mail->password = $this->config->get('config_smtp_password');
      $mail->port = $this->config->get('config_smtp_port');
      $mail->timeout = $this->config->get('config_smtp_timeout');				
      $mail->setTo($this->config->get('config_email'));
      $mail->setFrom($this->request->post['email']);
      $mail->setSender($this->request->post['name']);
      $mail->setSubject(html_entity_decode(sprintf($this->language->get('email_subject'), $this->request->post['name']), ENT_QUOTES, 'UTF-8'));
      $mail->setText(strip_tags(html_entity_decode('Имя:' . $this->request->post['name']. '/r/n Телефон:' . $this->request->post['phone']. '/r/n E-mail:' . $this->request->post['email']. '/r/n Сообщение:' . $this->request->post['message']. '/r/n Сообщение отправлено со страницы:' . $this->request->post['page'], ENT_QUOTES, 'UTF-8')));
      $mail->send();
      $json['success'] = 'OK';

    }	

    $this->response->setOutput(json_encode($json));	
  }


To which I answer Forbidden 403
The remaining requests to the server work adequately (native requests)
Where is my cant?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Evgrafovich, 2016-05-13
@Tantacula

I didn’t check the code in the modal () method, but if everything is correct there, then clear the opencart cache and vqmod, look at htaccess (you never know, maybe some changes were made there, but it’s unlikely), in extreme cases, restart the server. It should work, I wrote a quick order module in a similar way at one time, only in a separate controller.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question