Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5957495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:24:30+00:00 2026-05-22T18:24:30+00:00

I am trying to return a confirmation JSON object back to my AJAX function.

  • 0

I am trying to return a confirmation JSON object back to my AJAX function. For some reason, even though, the post is successful (200) the error callback function is always called. I am logging the returning JSON to a file for dubgging and it appears correct. I cannot figure out why this is happening. Can someone offer a suggestion?

PHP Controller Action (CI):

public function sendMail()
    {
        $senderName = trim($_POST['senderName']);
        $returnEmail = trim($_POST['returnEmail']);
        $message = trim($_POST['message']);


        if (valid_email($returnEmail))
        {
            send_email('email@email.com','Website Email From: '.$senderName, $message);
            $success = array('success'=>'Mail Sent');

            //Debugging to file
            $myFile = "testFile.txt";
            $fh = fopen($myFile, 'w') or die("can't open file");
            $stringData = json_encode($success);
            fwrite($fh, $stringData);
            fclose($fh);


            echo json_encode($success);
        }
        else
        {
            $errorMessage = array('error'=>'Invalid Email Address');
            echo json_encode($errorMessage);
        }
    }

}

JS:

$.ajax({
                   type: "POST",
                   url: "http://domain.com/index.php/mail/sendmail",
                   data: {senderName: senderName, returnEmail: senderAddr, message: message },
                   dataType: "JSON",
                   success: function(msg){
                     console.log(msg);
                   },
                   error: function(data){
                        alert("Something went wrong"); // possible that JSON wasn't returned
                    }
                 });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:24:31+00:00Added an answer on May 22, 2026 at 6:24 pm

    The problem was that I was not using a relative url for a target. I believe the issue was a cross domain scripting problem. I changed the url property to index.php/mail/sendmail and all is well.

    $.ajax({
                   type: "POST",
                   url: "index.php/mail/sendmail",
                   data: {senderName: senderName, returnEmail: senderAddr, message: message },
                   dataType: "JSON",
                   success: function(msg){
                     console.log(msg);
                   },
                   error:function (xhr, ajaxOptions, thrownError){
                        var x = xhr;
                        var y = ajaxOptions;
                        var z = thrownError;
                    }
                 });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create a list to return some JSON data to a view. Following
I'm trying to return data as an array to my .ajax function on success
I'm trying to return a dictionary from a function. I believe the function is
I am trying to return a bigger value like 1000 from my main function,
im trying to return a string value from a method inside my script tag
I'm trying to return a transparent GIF from an .aspx page for display within
I am trying to return an array Data Member from one smaller 2D Array
I am trying to return the result that I found in my query to
I'm trying to return an iterator for a vector in a templated class (I'm
I am trying to return the physical file path of a database's mdf/ldf files.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.