0

I am submitting data to a controller function via AJAX, doing what I need to do with the data, and trying to echo it back out to the ajax function.

The issue I am having, is the controller is dumping out the error message and trying to redirect me to the actual function. Obviously the function doesn't have a view, which results in a blank white screen with the response echoed out in the top left corner.

Here is the ajax:

    $('#submit_new_split_promo').on('click',function(e){
        e.preventDefault();
        var id = $(this).data('id');

        $('#d_overlay').show();
        form = {};
        $.each($('#promo-mail-split-add-'+id).serializeArray(),function(k,v){
            form[this.name] = this.value;
        });

        $.ajax({
            url:$('#promo-mail-split-add-'+id).attr('action'),
            type:"POST",
            dataType: "json",
            data: form
        }).done(function(result){
            var res = JSON.parse(result);

            if (res == 'Duplicate') {
                $('#ms-promo').css('border','3px solid red');
                $('#ms-promo').effect('shake');
                $('#dynamodal-unique-title').text('That code has been used. Please enter a new Promo Code.');

                $('#dynamodal-unique-title').text('That code has been used. Please enter a new Promo Code.').css('color','red').delay(2000).queue(function(next){
                    $('#dynamodal-unique-title').text('Create Mail Split Promo');
                    next();
                });

                return false;       
            }

            $('#mail_split_promo_'+id).modal('toggle');
            if (res == false) {
                alert('Mail Split Promo did not save. Please try again.');
            } else {
                $('#add-promo-to-split-'+id).prop('disabled',true);
                $('#promo-view-abled-'+id).hide();
                $('#promo-view-disabled-'+id).show();
                $('#promo-view-disabled-'+id).prop('disabled',false);
            }

        }).fail(function(){
        }).always(function(){
            $('#d_overlay').hide();
        });
    });

Here is the Controllers code

public function addpromo() {
    $this->Authorization->skipAuthorization();
    $this->request->allowMethod(['get','post']);
    $this->autoRender = false;

    $data = $this->request->getData();
    $mail_split_id = $data['mail_split_id'];

    $code = $data['code'];

    $result = false;

    $doesExist = $this->Promos->findByCode($code)->toArray();
    if ($doesExist) {
        $result = 'Duplicate';
    }

    if ($result !== 'Duplicate') {

        $MailSplits = $this->getTableLocator()->get('MailSplits');
        $mailSplit = $MailSplits->get($mail_split_id);

        $entity = $this->Promos->newEmptyEntity();
        foreach ($data as $key => $val) {
            $entity->$key = $val;
        }

        $entity->record_count = $mailSplit->record_count;
        $result = $this->Promos->save($entity);
        if ($this->get_property($result,'id')) {
            $promo_id = $result->id;

            $MailSplits = $this->loadModel('MailSplits');
            $mentity = $MailSplits->get($mail_split_id);
            $mentity->promo_id = $promo_id;

            $updated = $MailSplits->save($mentity);
            if ($this->get_property($updated,'id')) {
                $result = true;
            } else {
                $result = false;
            }

            $output = []; 
            exec(EXEC_PATH.'AddPromoToRecordSplits '.$promo_id,$output);

        } else {
            $result = false;
        }
    }

    ob_flush();
    echo json_encode($result);
    exit(0);
} 

The URL it is trying to redirect me to is: /promos/addpromo when I really just need to stay on the same page, which would be /mail-jobs/view Response dumped to browser

A couple of things to note: I have tried adding the function to the controllers policy, and actually authorizing an initialized entity. This has no effect and does not change the issue I am facing.

Something that is more frustrating, I have essentially the same code (ajax structure and controller structure) for other forms on the page, and they work just fine. The only difference seems to be any form that utilizes ajax that is on the page on render, works just fine. The ajax functions I am having an issue with, all seem to be from the forms rendered in Modals, which are different elements. Every form in a modal / element, gives me this issue and that's really the only pattern I have noticed.

Any help is greatly appreciated, I know it's an odd and vague issue.

Thank you!

Telah32
  • 9
  • 4
  • I don't see anything in either of these that should be doing a redirect. The fact that you're getting a white page would seem to indicate that it might be submitting the form normally instead of via Ajax, is that a possibility? – Greg Schmidt May 10 '21 at 05:05
  • 1
    Side note, [**controllers should not echo data**](https://stackoverflow.com/questions/42378793/how-to-output-custom-http-body-contents-with-cakephp-3-4-echoing-causes-unable/42379581#42379581), and especially not terminate the process, which will cause the whole middleware stack to not run its repsponse processing logic! – ndm May 10 '21 at 11:27
  • See maybe [this](https://www.dereuromark.de/2014/01/09/ajax-and-cakephp/) for a few tips, also linked is the Ajax plugin which also might make rendering and returning HTML snippets maybe a bit easier. – mark May 12 '21 at 10:41

0 Answers0