【问题标题】:Convert from curl to ajax从 curl 转换为 ajax
【发布时间】:2018-02-13 15:54:04
【问题描述】:

我想就我的 ajax 代码获得一些帮助。我想移动 index.html 中的 curl 代码以使用 ajax,但我不知道如何将 curl 转换为 ajax 代码。

索引.html:

<script>
$button.click(function(e) 
{
   e.preventDefault();
   var emptyMail = true;
   var email = $emailInput.val().trim();
   var $emailInput_1 = $("#email").val();    

   $(document).ready(function()  
       $(this).val("SUBMITTING...");
       $("form").submit();

       $.ajax({
         url: "post.php",
         type: 'POST'
        });
    });
</script>

这里是post.php

<?php
function _curl($url, $post = "", $headers = "")
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_POST, ($post && is_array($post))? 1 :0);
    if ($post && is_array($post))
    {
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
    }
    if ($headers && is_array($headers))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "/dev/null");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "/dev/null");
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $result = curl_exec($ch);
    return $result;
    curl_close($ch);
}
if ($_POST)
{
    _curl("http://example.com/land/formFillReturnV5b.php", array(
    "name"=>"groups_name",
    "email"=>$_POST['email'],
    "phone"=>"",
    "ip"=> $_SERVER['SERVER_ADDR'],
    "reff"=>"",
    "page"=>"Q6",
    "link"=>"https://example.com/meme",
    "notes"=>"",
    "MID"=>"39918",
    "LID"=>"",
    "ARData"=>"meme",
    "cookie"=>""), array("X-NewRelic-ID" => "VQQBVl9aDRABUFJbAQkOUQ==", "X-Requested-With" => "XMLHttpRequest", "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"));
}
?>

我想变成这样:

<script>
    $button.click(function(e) 
    {
       e.preventDefault();
       var emptyMail = true;
       var email = $emailInput.val().trim();
       var $emailInput_1 = $("#email").val();    

       $(document).ready(function()  
           $(this).val("SUBMITTING...");
           $("form").submit();

           $.ajax({
             url: "http://example.com/land/formFillReturnV5b.php",
             type: 'POST',
             "name"=>"groups_name",
             "email"=>$_POST['email'],
             "phone"=>"",
             "ip"=> $_SERVER['SERVER_ADDR'],
             "reff"=>"",
             "page"=>"Q6",
             "link"=>"https://example.onlinesalespro.com/meme",
             "notes"=>"",
             "MID"=>"39918",
             "LID"=>"",
             "ARData"=>"meme",
             "cookie"=>""), array("X-NewRelic-ID" => "VQQBVl9aDRABUFJbAQkOUQ==",      "X-Requested-With" => "XMLHttpRequest", "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"));
            });
        });
    </script>

当我尝试它时,它不会工作。如果你能告诉我一个例子,我可以如何使用 ajax 代码将数据发送到服务器,那就太好了。

【问题讨论】:

  • 所以查看data的jQuery文档,cookie应该在浏览器中设置并默认发送到服务器。
  • “不起作用”是一个无用的问题陈述。发生什么了? javascript 控制台中是否有错误?您是否查看过浏览器的开发人员工具“网络”选项卡?那里显示了什么?
  • 这里有太多错误,不知道从哪里开始(投票结束时“过于宽泛”)。您似乎不理解 JavaScript 语法并试图做一些用客户端 JS 完全不可能做的事情,这于事无补。您应该阅读 JavaScript、jQuery 和 Ajax 的介绍性教程。
  • 提示:编程时,请从尽可能小的事情开始以使其正常工作。对您来说,这是一个简单的 AJAX 调用(不是一个复杂的调用,也不是一个非现场 URL)。然后,慢慢添加您需要的功能,一次一个,当它中断时,您会确切地知道原因,因为这是您添加的最后一件事。

标签: javascript php ajax curl


【解决方案1】:

使用Documentation

示例代码

<script>
$button.click(function(e) 
{
   e.preventDefault();
   var emptyMail = true;
   var email = $emailInput.val().trim();
   var $emailInput_1 = $("#email").val();    

   $(document).ready(function()  
       $(this).val("SUBMITTING...");
       $("form").submit();

       $.ajax({
         url: "http://example.com/land/formFillReturnV5b.php",
         type: 'POST',
         data: {name: "groups_name", email: "text@email.ru" /* and other parametrs*/ }
         success: function(result) {
          console.log(result);
         }
        });
    });
</script>

【讨论】:

  • 非常感谢,那么 cookie 呢?
  • 根据我使用 Ajax 的经验,传递 cookie 从来没有问题(如果您向服务器而不是服务器发出请求)它们总是自动传递。关于您在标题中转换的所有其他参数,请查看文档或谷歌
猜你喜欢
  • 2016-01-08
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 2018-02-15
  • 2016-10-10
  • 2021-02-14
  • 2017-05-18
相关资源
最近更新 更多