Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Remove below code from <code>ajax</code> call and try:</p> <pre><code>contentType: "application/json; charset=utf-8", </code></pre> <p><code>jQuery.parseJSON(data)</code> is not needed as <code>Content-Type</code> is set and encoded in <code>json</code> by below code:</p> <pre><code>header("Content-Type: application/json; charset=utf-8", true); echo json_encode($response); </code></pre> <p>Updated code with test data:</p> <p>JS</p> <pre><code>$.ajax({ type: 'POST', dataType: "json", url : 'beemailer.php', data: {name: 'rai', email: 'rai@test.com', message: 'Test message', subject: 'Test subject'}, success : function(data) { console.log(data); alert(data); }, error: function(error, x, y) { console.log(error); alert(x, y); } }); </code></pre> <p>PHP (beemailer.php)</p> <pre><code>&lt;?php $to = "ashish_sharma307@hotmail.com"; $subject = $_POST['subject']; $message = $_POST['message']; $headers = "From:" . $_POST['name'] . "(" . $_POST['email'] . ")"; mail($to, $subject, $message, $headers); $response = "The mail has been sent. Thank You the valid mail."; header("Content-Type: application/json; charset=utf-8", true); echo json_encode($response); ?&gt; </code></pre> <p>Example of object:</p> <pre><code>$result = array(); $result['status'] = 'success'; $result['message'] = 'The mail has been sent. Thank You the valid mail.'; header("Content-Type: application/json; charset=utf-8", true); echo json_encode($result); </code></pre> <p>JS to access object:</p> <pre><code>$.ajax({ type: 'POST', dataType: "json", url : 'beemailer.php', data: {name: 'rai', email: 'rai@test.com', message: 'Test message', subject: 'Test subject'}, success : function(data) { alert('status: ' + data.status + '\nmessage: ' + data.message); }, error: function(error, x, y) { console.log(error); alert(x, y); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload