Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将fetch转换为ajax以用于条带集成_Javascript_Php_Fetch_Stripe Payments - Fatal编程技术网

Javascript 将fetch转换为ajax以用于条带集成

Javascript 将fetch转换为ajax以用于条带集成,javascript,php,fetch,stripe-payments,Javascript,Php,Fetch,Stripe Payments,我在移动google chrome上的fetch有问题。我想尝试将其转换为ajax。我使用它在php中实现条带。这是我使用fetch所做的: var createCheckoutSession = function(planId) { var plan = { plan_id: planId }; var data = new FormData(); // we call the create my session page by

我在移动google chrome上的fetch有问题。我想尝试将其转换为ajax。我使用它在php中实现条带。这是我使用fetch所做的:

  var createCheckoutSession = function(planId) {
    var plan = {
          plan_id: planId
      };
  
      var data = new FormData();
 // we call the create my session page by providing a Plan ID which in turn will return a session object whose ID will be useful later on to redirect to checkout.
      data.append( "plan", JSON.stringify( plan ) );
    return fetch("/create-my-session", {
      method: "POST",
      body: data
    }).then(function(result) {
      console.log(result);
      return result.json();
    });
  };

在“创建我的会话”页面中,我有以下代码:

$stripeService = new StripeService();
    $plan = json_decode($_POST["plan"]);
    $planId = $plan->plan_id;
    $session  = $stripeService->createCheckoutSession($planId);
    echo json_encode($session);
    

单击此按钮时执行上述代码:

   $('#subscribe').on('click',function(e){ 
        createCheckoutSession(MyChosenPlanID).then(function(data) {
          stripe.redirectToCheckout({
      //we redirect the user to a checkout page hosted by stripe that uses the session ID returned above
          sessionId: data.id
          }).then(handleResult);
        });
          });
到目前为止,我在转换为ajax方面所做的工作:

 var createCheckoutSession = function(planId) {
    var plan = {
          plan_id: planId
      };
  
      var datastream = new FormData();
      datastream.append( "plan", JSON.stringify( plan ) );

     $.ajax({

      type: "POST",
      url: "/create-my-session",
      data: {
        'info': datastream,
    },
      dataType: 'json',
      success: function(data){
          
          var sessionobj = data;
          
      },
      error:function(response)
      {
          console.log("Data sending failed");
          console.log(response);
      }
  });
     
    
    return sessionobj ;
    }).then(function(result) {
      console.log(result);
      return result.json();
    });
  };

我保留了所有其他内容。

您能否澄清您遇到的确切问题,该代码是否没有达到您的预期?总体而言,我对这个目标有点困惑,因为
fetch
和jQuery的
$。据我所知,ajax
做的事情完全相同,它们是XMLHTTPRequest。@karllekko感谢您的回答。取货机运转良好。事实上,我试图用php在Drupal中实现这一点,我的问题是createmysession链接会检查用户是否登录,并据此进行操作。出于某种原因,如果用户在订阅按钮所在的页面上登录din,如果在Chrome for android上进行测试,则会在创建我的会话页面上显示为注销。我在台式机和Iphone上测试了它,它工作得很好,所以我怀疑如果它与android不兼容,那么fetch就是原因。你的链接显示fetch在android上工作。我认为您的身份验证问题与使用
fetch
与使用
ajax
没有任何关系,因为它们最终只会使用相同的cookie发出相同的请求,所以我会进一步调试并查看其他地方。