我们正在尝试与需要JSON格式数据的远程API进行通信。我们试图如下提交此使用JQuery,但都拿到SOP错误:如何使用Coldfusion 9将JSON发布到远程URL?
<script type="text/javascript">
$.ajax({
username: 'username',
password: 'password',
url: "https://api.e2ma.net/123/members/add",
dataType: 'json',
type: 'POST',
data:
{
"fields": {
"name_first": "Name"
},
"email": "[email protected]"
}
});
</script>
不幸的是,他们的API 不支持JSONP。
我们怎样才能将这个JSON数据直接从Coldfusion 9中发布到他们的远程API?
另外,我们需要做些什么来从他们的API解析回应?
下面是该文档的链接:http://myemma.com/api-docs/
这里是一个简单的代码片段从这些文档添加新成员的呼叫:
import_single_member(account_id)
POST /#account_id/members/add
Adds or updates an audience member
Parameters:
email (string) – Email address of member to add or update
fields (dictionary) – Names and values of user-defined fields to update
group_ids (integer) – Optional. Add imported members to this list of groups.
signup_form_id – Optional. Indicate that this member used a particular signup form.
Returns:
The member_id of the new or updated member, and whether the member was added or an existing member was updated
Example:
POST /123/members/add
{
"fields": {
"first_name": "Benjamin"
},
"email": "[email protected]"
}
{
"added": true,
"member_id": 1024
}
感谢。
你想设置,抓住远程JSON数据服务器上的代理,你再打电话给当地的代理使用JavaScript,它获取远程数据,并将其返回给浏览器。 jQuery提供了使用'$ .parseJSON()'解码JSON的能力。您的代理仅充当JavaScript和远程服务之间的中介,以避免跨域策略问题。 – Jasper
没有办法通过直接从CF中发布这个JSON数据来完全跳过JQuery/JS(从而需要代理)吗? –
当然,用CF抓取远程数据,然后以任何你想要的格式输出。我只提到了JavaScript部分,因为你用jQuery标记了问题。如果没有JavaScript,您将不得不重新加载整个页面以获取新数据,我认为只使用服务器端脚本是不利的。 – Jasper