jquery / ajax form - serverside php but get Cross domain warning

I'm stuck at this problem half the day and i'm totally out of options now.

I have a form that is submitted via a click handler to a php script that is in the SAME directory BUT everytime I try to send it, i get a cross domain script warning.

Please Help, you are my only hope ( since nobody else seem to have that problem according to google and i'm only half a hour from trowing my laptop out of the window).

thanks for your help,

Error:

POST http://mydomain/contactscript.php 500 (Internal Server   Error)jquery.js:8625 n.ajaxTransport.k.cors.a.crossDomain.sendjquery.js:8161     
n.extend.ajaxcontact.js:12 (anonymous function)jquery.js:4430 n.event.dispatchjquery.js:4116 n.event.add.r.handle

My Ajax Call:

$('#kontaktformular button.cbp-mc-submit').click(function(event){
    event.preventDefault();
    var myForm = $("#kontaktform");
    var button = $("#kontaktform button.cbp-mc-submit");
    if($('#firstname').val() != "" && $('#firstlast').val() != "" && $('#email').val() != "") {
        $.ajax( {
            url: 'contactscript.php',
            type: 'POST',
            dataType: 'html',
            data: myForm.serialize(),
            beforeSend: function() {
                // alert.fadeOut();
                button.html('abgesendet....'); // change submit button text
            },
            success:function(result){
                button.html('Kontaktanfrage absenden');
            }
        });
    }
    else{

        $('#first_name , #last_name, #email1').filter(function() { return $(this).val() == ""; }).css({'background': "#ffa5a5", 'color' : 'white'});
    }
});

AJAX compiled Message (I guess?)

Remote Address:xx.xx.xx.xx.xx:80
Request URL:http://domain/contactscript.php
Request Method:POST
Status Code:500 Internal Server Error
Request Headersview source
Accept:text/html, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Content-Length:90
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:_ga=GA1.2.878328721.1423595127
Host:domain.de
Origin:http://domain.de
Pragma:no-cache
Referer:http://domain.de/contact.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2)              AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
firstname:name1
lastname:name2
phone:
email:[email protected]
subject:
comments:textyay
Response Headersview source
Connection:close
Content-Length:0
Content-Type:text/html
Date:Mon, 02 Mar 2015 03:32:00 GMT
Server:Apache/2.2.29 (Unix)
X-Powered-By:PHP/5.3.29

The ContactScript

<?php
if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ){
if (isset($_POST['lastname'],$_POST['firstname'] ,$_POST['email']) {



   $firstname  =    check_input($_POST['firstname']);
   $lastname   =    check_input($_POST['lastname']);
   $email      =    check_input($_POST['email']);
   $phone      =    check_input($_POST['phone']);
   $callback   =    check_input($_POST['callback']);
   $subject    =    "Kontaktanfrage"  .check_input($_POST['subject']);
   $comments   =    check_input($_POST['comments']);
   $name    =    "$firstname $lastname";
   $message =  "NAME: $firstname $lastname  "   ."<br>\n";
   $message .= "TELEFONNUMMER:  $phone"   ."<br>\n";
   $message .= "RÜCKRUF: $callback"   ."<br>\n";
   $message .= "EMAIL: $email "    ."<br>\n";
   $message .= "BETREFF: $subject"     ."<br>\n";
   $message .= "NACHRICHT: " .$comments   ."<br>\n";  



  $sent = sendmail($email, $name, $subject, $message);

  if ($sent) {
  echo 'Message sent!';
   } else {
    echo 'Message couldn\'t sent!';
  }
 }
  else {
  echo 'All Fields are required';
  }
  return;
 }





function check_input($data)
 {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
 }

function sendmail($from_mail, $from_name, $subject, $message){
  $header = array();
  $header[] = "MIME-Version: 1.0";
  $header[] = "From: {$from_name}<{$from_mail}>";
 /* Set message content type HTML*/
  $header[] = "Content-type:text/html; charset=iso-8859-1";
  $header[] = "Content-Transfer-Encoding: 7bit";
 if( mail('[email protected]', $subject, $message, implode("\r\n",    $header)) ) return true; 
}



?>

Answers:

Answer

ajax response has indicate your server script is error, and yes your php script has error in line 2.. you missed one closing ) in if statement..

if (isset($_POST['lastname'],$_POST['firstname'] ,$_POST['email']) { 

to

if (isset($_POST['lastname'],$_POST['firstname'] ,$_POST['email'])) {
Answer

My Chrome Dev Tools kept activating breakpoints with every XHR request to server side php.

Short answer - Sources tab, uncheck XHR Breakpoints - Any XHR (at right)

Long answer (why this answer belongs here)

I incorrectly thought it was a cross domain issue because:

  • The console kept referencing jquery.js:8630
  • xHR obj in the console has this property: n.ajaxTransport.k.cors.a.crossDomain.send
  • All web searches pointed to cross domain issues.

Hope this helps someone!

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.