0

After getting the planning (Get projects from Teamweek in jQuery) successfully via the API i now want to create feed teamweek a project.

The following code works (according to the response i get) but i can't find the project when i log in to teamweek.

Teamweek API Documentation

create_project = function(_project)
{
    console.log('Sending new project to teamweek', _project);
    return $.ajax(
    {
        url         : api_url + app.model.teamweek_account_id + '/projects/new.json',
        type        : 'POST',
        dataType    : 'jsonp',
        cache       : false,
        data        : {
                             "active"       : true,
                             "client_id"    : _project.client,
                             "color"        : "#f00",
                             "name"         : _project.title,
                             "id"           : _project.project_number
                      },

        beforeSend: function (request) { return request; },

        success:function(response) {
            return response;
        },

        error: function (jqXHR, textStatus, errorThrown) {
            console.error('error getting teamweek planning:', jqXHR, textStatus, errorThrown);
        },
    }); 
},

Response:

{
    "project": {
        "name": "text",
        "client_id": "integer",
        "color": "string",
        "active": "boolean"
    },
    "links": [{
        "href": "https://teamweek.com/api/v2/:my_account_id/projects",
        "rel": "create",
        "method": "POST"
    }, {
        "href": "https://teamweek.com/api/v2/:my_account_id/projects/:id",
        "rel": "update",
        "method": "PUT"
    }, {
        "href": "https://teamweek.com/api/v2/:my_account_id/projects/:id",
        "rel": "read",
        "method": "GET"
    }, {
        "href": "https://teamweek.com/api/v2/:my_account_id/projects/:id",
        "rel": "delete",
        "method": "DELETE"
    }, {
        "href": "https://teamweek.com/api/v2/:my_account_id/projects",
        "rel": "list",
        "method": "GET"
    }]
})
Community
  • 1
  • 1
Mettin Parzinski
  • 838
  • 1
  • 7
  • 13

1 Answers1

1

Your endpoint is incorrect, from the API docs (https://github.com/toggl/teamweek_api_docs/blob/master/chapters/projects.md#create-project)

POST https://teamweek.com/api/v2/:account_id/projects.json

Your response should be the created project, not the template...

refiito
  • 56
  • 1
  • Are you sending a POST request? GET /projects returns the project list... It looks like you're doing exactly that. Use POST, also, you might try using pure json, not jsonp – refiito Nov 19 '13 at 09:13
  • POST. pure json gives a cross-domain error... I've put my code in a fiddle: http://jsfiddle.net/mettin/4U8WH/1/ – Mettin Parzinski Nov 19 '13 at 13:15
  • Well, it seems using JSONP is your culprit. From this thread - http://stackoverflow.com/questions/2699277/post-data-to-jsonp (hence, you cannot do a POST request with JSONP). If you want to build an interface inside a browser and doing direct requests, then either forget about POST/DELETE/PUT request (get only, just display data) or add a some sort of middle plate to do the requests for you. If this doesn't suit your ideas, well, contact support@teamweek.com about getting an exception for your project in CORS headers (why, where etc)... – refiito Nov 19 '13 at 20:27