0

Can I use $.ajax() to send POST to PHP file on other domain? A have a error message in $.ajax(). In Chrome or Firefox - email send. In IE - email is not send.

This is HTML file

<form name="order" method="post"action="http://www.site.ru/images/stories/controller.php">
<label>1. Mark*:</label>
    <select name="mark" class="inputbox">
        <option ="selected"></option>
        <option>Audi</option>
        <option>BMW</option>
    </select>
<label>2. Model*:</label>
    <input type="text" name="model" size="30" class="inputbox"/>
<label>3. Year*:</label>
    <select name="year" class="inputbox">
        <option ="selected"></option>
        <option>2011</option>
        <option>2010</option>
    </select>
<label>4. Value, l*:</label>
    <input type="text" name="value" class="inputbox" size="30"/>
<label>5. Transmission*:</label>
    <select name="transmission" class="inputbox">
        <option ="selected"></option>
        <option>Manual</option>
        <option>Automatic</option>
    </select>
<label>6. VIN:</label>
    <input type="text" name="vin" size="30" value="" class="inputbox" maxlength="17" />
<label>7. E-mail*:</label>
    <input type="text" name="email" size="30" class="inputbox" />
<label>Note*:</label>
    <textarea cols="50" rows="5" name="text" id="contact_text" class="inputbox"/</textarea>
<input type="button" value="Send" onclick="valid();"></input>
</form>

<script src="http://admin.abcp.ru/common.jscripts/jquery/jquery-1.6.2.min.js"></script>
<script language="JavaScript" type="text/javascript">
function sendEmail(mark, model, year, value, transmission, vin, email, text){
    $.ajax({
        url: "http://www.site.ru/images/stories/controller.php",
        type : "POST",
        data : {
            mark : mark,
            model : model,
            year : year,
            value : value,
            transmission : transmission,
            vin : vin,
            email : email,
            text : text
        },
        success: function(){
            alert('Success.');
        },
        error : function (){
            alert('Error.');
        }
    });
}
</script>
<script language="JavaScript" type="text/javascript">
function valid(){
if (document.order.mark.value == ""){
    alert("Mark is empty.");
    document.order.mark.focus(true);
    return false;
}
if (document.order.model.value == ""){
    alert("Model is empty.");
    document.order.model.focus(true);
    return false;
}
if (document.order.year.value == ""){
    alert("Year is empty.");
    document.order.year.focus(true);
    return false;
}
if (document.order.value.value == ""){
    alert("Value is empty.");
    document.order.value.focus(true);
    return false;
}
if (document.order.transmission.value == ""){
    alert("Transmission is empty.");
document.order.transmission.focus(true);
    return false;
}
if (document.order.email.value == ""){
    alert("E-mail is empty.");
document.order.email.focus(true);
    return false;
}
if (document.order.text.value == ""){
    alert("Text is empty.");
document.order.text.focus(true);
    return false;
}
sendEmail(document.order.mark.text, 
    document.order.model.text,
    document.order.year.text, 
    document.order.value.text, 
    document.order.transmission.text,
    document.order.vin.text,
    document.order.email.text, 
    document.order.text.text);
}
</script>

This is PHP file controller.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Обратная связь</title>
</head>
<body>
<?php
$from = $email;
$to = "[email protected]";
$subject = "PINAZA.RU - Запрос по $mark";
$message = "Марка автомобиля: $mark \nМодель автомобиля: $model \nГод выпуска: $year \nОбъем двигателя, л: $value \nТип КПП: $transmission \nVIN-код: $vin \nНеобходимые детали:     $text";
$headers = "Content-type: text/plain; charset = utf-8";
mail("$to", "$subject", "$message", "from: ".$from."\r\n"."$headers");
?>
</body>
</html>

7 Answers 7

2

You cannot use ajax to post to different domain.

Refer: http://en.wikipedia.org/wiki/Same_origin_policy

You can use proxy instead. Post this data to your php/asp.net file (which resides on same domain) and from that php/asp.net file post data to another domain.

Sign up to request clarification or add additional context in comments.

Comments

1

in the php file that is placed on the remote domain place this line at the top

<? php
header('Access-Control-Allow-Origin: *');

$foo = $_POST['foo'];
echo json_encode($foo);    
?>

an do the post

$.ajax({
url:'/path',
type:'POST',
data:{foo:'bar'},
crossDomain:true,
success:function(data){
 console.log(data);// you will get bar 
});

Comments

1

You will need to use jsonp for achieving this on all platforms

Comments

0

yes you can if you will use dataType: "jsonp". Look at this http://devlog.info/2010/03/10/cross-domain-ajax/

Comments

0

I think you get cross domain exception. If you add to POST request crossDomain:true it will work but not all browsers support it. more about this you can find here http://api.jquery.com/jQuery.ajax/ maybe it help

Comments

0

Normally you can't do any cross-domain POST via $.ajax() or JSONP call.

The reason it works on Chrome & Firefox because they implemented Cross-domain HTTP request

You have to put this code on the remote server (controller.php)

<?php header('Access-Control-Allow-Origin: *'); ?>

but then again, this doesn't help you with IE problem but it can explain why it's doesn't work. If you want it to work on IE you have to configure IE browser to allow "Access data across domain" under "Security Level"

Comments

0

define form id and remove action in the form like below

<form name="order" method="post"action="#" id="myform" onsubmit="return submitForm();">

and add a function in js to send post request to server by using ajax

<script> 
function submitForm() {
    var form_data = new FormData(document.getElementById("myform"));        

    form_data.append("label", "WEBUPLOAD");   
       $.ajax({
          url: "YOUR URL HERE",
          type: "POST",
          data: form_data,
          processData: false,  // tell jQuery not to process the data
          contentType: false   // tell jQuery not to set contentType       

    }).done(function( data ) {
        console.log(data);
        //Perform ANy action after successfuly post data
    });   return false;      
} 
</script>

I got code sample Send POST REQUEST BY USING AJAX

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.