doukengzi3517 2014-03-16 15:18
浏览 50

如何正确格式化basecamp的ajax POST

I can easily create a message from bash shell utilizing the curl method described in the basecamp api docs. However, my app is not written in php so I'd like to be able to access the basecamp sever via a basic ajax post. Unfortunately, I don't seem to be able to translate the curl statement into the ajax post. I though this would suffice:

function callBasecamp() {
    var parameters = {  
              user:"[my_basecamp_username]",
              pass:"[my_basecamp_password]",
              userAgent: '[my_app] (my_email)',
              contentType: 'application/json; charset=utf-8',
      data: ({ "subject": "This is a Test Message", "content": "This is test content. Please disregard if notified." }),
             };
    var data = JSON.stringify(parameters);
    $.ajax({
        type: "POST",
        data: data,
        dataType: 'json',
    url: "../../../../site_media/proxy.php?url=https://basecamp.com/[account_id#]/api/v1/projects/[project#]/messages.json?" + data,
        traditional: true,
        success: function(data){
            console.log(data);
        }
    });
}

but though my dev server returns an HTTP 200 216 response, basecamp doesn't create the message and I don't see any returned data. I am using a php proxy to circumvent django csrf issues:

proxy.php

<?php
// File Name: proxy.php
if (!isset($_POST['url'])) die();
$url = urldecode($_POST['url']);
$url = 'https://' . str_replace('https://', '', $url); // Avoid accessing the file system
echo file_get_contents($url); 

any ideas on where my difficulty might be?

  • 写回答

1条回答 默认 最新

  • doujiao9866 2014-03-16 15:24
    关注

    The proxy doesn't forward correctly. Try with added headers on your proxy.php:

    header('Access-Control-Allow-Origin: *');
    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');
    

    For JSON-P: header('Content-Type: application/javascript');

    Debugging Helper for file_get_contents()

    $url1 = 'https://basecamp.com/999999999/api/v1/projects.json';
    $url2 = 'https://basecamp.com/2052106/api/v1/projects/1450272/messages.json';
    
    function send($url, $payload) {
      $ctx = stream_context_create(
        array(
          'http'=>array(
            'header'=> "Content-type: application/json
    "
                     . "Access-Control-Allow-Origin: *
    "
                     . "User-Agent: myApp (app@app.com)
    "
                     . "Accept: xml/*, text/*, */*
    ",
            'method'=> 'GET',
            //'content'=> $payload,
            'ignore_errors' => true
          )
        )
      );
    
      $result = file_get_contents($url, 0, $ctx);
    
      var_dump($http_response_header);
    
      return $result;
    
    }
    
    // The expected return is:
    // There's no Basecamp account at this address. ....
    echo send($url1, '');
    
    // expected return: {"status":"404","error":"Not Found"}
    echo send($url2, '');
    

    Usage for a test-request with payload:

    Notice: you might switch GET to POST inside stream_content options:

    $url = 'https://basecamp.com/2052106/api/v1/projects/1450272/messages.json';
    
    $payload = '{"user":"123",
                 "pass":"123",
                 "userAgent":"test test@test.com",
                 "contentType":"application/json; charset=utf-8",
                 "data": {
                    "subject":"This is a Test Message",
                    "content":"This is test content. Please disregard if notified."}
                 }';
    
    echo send($url, $payload);
    

    An alternative is to use cURL from within PHP: https://stackoverflow.com/a/15395769/1163786

    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程