2013-07-10 29 views
1

對於我使用的是外部API,我一般不喜歡這樣的捲曲要求:任何方式使用forge.request.ajax使這個cUrl請求?

curl -H "X-Auth-Token: 123123123123123" -X PUT -d '{"$set":{"title":"Person 1a"}}' http://domain.com/collectionapi/persons/123123123123123 

有什麼辦法來港這forge.request.ajax?在我的努力,到目前爲止,在瀏覽器中,我得到500錯誤:

http://localhost:3000/_forge/proxy/moc/edakcart/ 

響應

{"error":"SyntaxError: Unexpected token %"} 

我的代碼是:

forge.request.ajax({ 
    type: 'POST', 
    url: 'http://domain.com/collectionapi/persons/123123123123123/', 
    data: {"$set":{"title":"Person 1b"}}, 
    dataType: 'json', 
    headers: { 
     'X-Auth-Token': '123123123123123' 
    }, 
    success: function(data) { 
     forge.logging.info('[trackadeApi] Updated x to '+ data.x); 
    }, 
    error: function(error) { 
     forge.logging.info('[trackadeApi] Failed to update x: '+ error.message); 
    } 
}); 

感謝您的時間。

回答

1

下面是一個有效的例子。感謝託德https://github.com/crazytoad

forge.request.ajax({ 
    type: 'PUT', 
    url: 'http://domain.com/collectionapi/persons/2JMcfXZ3PJjESGGLX?auth-token=123123123', 
    contentType: 'application/json; charset=utf-8', 
    data: JSON.stringify({"$set":{ 
     "title": 'Person 1b' 
    }}), 
    dataType: 'jsonp', 
    success: function(data) { 
     forge.logging.info('[trackadeApi] Updated'); 
    }, 
    error: function(error) { 
     forge.logging.info('[trackadeApi] Failed to update'); 
    } 
});