请求失败"否'访问控制 - 允许 - 来源'报头"

时间:2015-10-22 15:44:03

标签: javascript html ajax

我使用Get Response(电子邮件客户端)创建了一个表单,并尝试在表单上使用ajax来创建自定义成功/失败消息。代码工作正常。但是当我将ajax链接到表单时它停止工作,我收到以下错误消息。非常感谢任何帮助。

  

错误:无法加载XMLHttpRequest   https://app.getresponse.com/add_subscriber.html。没有   '访问控制允许来源'标题出现在请求的上   资源。起源' http://localhost:8888'因此是不允许的   访问。

<form id="email-form-2" name="email-form-2" accept-charset="utf-8" action="https://app.getresponse.com/add_subscriber.html" method="post">
 <input type="text" placeholder="Your Name" name="name">
     <input type="text" placeholder="Your Email" name="email">
       <textarea placeholder="Your Message..." name="custom_comment"></textarea>
          <input type="hidden" name="campaign_token" value="XYZ" />
          <input type="submit" value="Send Message" data-wait="Please wait...">
        </form>
        <div>
          <p>Thanks for contacting us :)</p>
        </div>
        <div>
          <p>Darn. It didn't work. Give it another shot and see what happens :)</p>
        </div>

         <script type="text/javascript">
          $(function () {
           $('#email-form-2').submit(function (e) {
           e.preventDefault();
           $.ajax({
            url: this.action,
            data: $(this).serialize(),
            type: 'POST'
            }).done(function () { 
           $("#email-form-2+div.w-form-done").show();
           $("#email-form-2+div.w-form-fail").hide();
           $("#email-form-2").hide();
          })
           .fail(function () { 
           $("#email-form-2+div.w-form-done").hide();
           $("#email-form-2+div.w-form-fail").show();
           $("#email-form-2").show();
         });
       });
   });
</script>

1 个答案:

答案 0 :(得分:1)

好的,我建议您在服务器端使用您喜欢的任何语言cURL。现在我可以建议你cURL和PHP。请在proxy.php中尝试此操作:

<?php 
    // create curl resource 
    $ch = curl_init(); 

    // set url 
    curl_setopt($ch, CURLOPT_URL, "https://app.getresponse.com/add_subscriber.html");

    //return the transfer as a string 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // $output contains the output string
    $output = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch);

    // output
    die($output);
?>

现在,您可以在AJAX请求中使用proxy.php,因为它位于同一个域中。你还有另一种方式:

<?php
    die(file_get_contents("https://app.getresponse.com/add_subscriber.html"));
?>