我正在尝试使用ractive.js向API添加JSON数据/对象,但我没有得到任何结果。它向我显示了一个“没有内容”的回复。不确定我目前缺少什么。我正在为这个项目使用ractive.js。
这是我一直在研究的示例代码。
app.on('addLead', function() {
var proxy = app.get('proxy');
var endpoint = 'account/leads/';
var rt = 'POST';
var url = proxy+'?endpoint='+endpoint+'&rt='+rt;
var data = {
"isProfiled": true,
"isBaluarte": 0,
"isVertical": 0,
"isAffiliate": 0,
"isBranch": 0,
"id": 0,
"dateRequested": "2017-01-12T07:13:19.717Z",
"accountIdFk": 0,
"accountLeadId": 1285689131,
"accountName": "Noel String",
"assignedEmployeeIdFk": 0,
"unitNumber": "string",
"floorNumber": "string",
"buildingName": "string",
"streetNumber": "string",
"streetName": "string",
"barangayIdFk": 0,
"cityTownIdFk": 0,
"provinceIdFk": 0,
"regionIdFk": 0,
"leadsIdentifier": "string",
"salesRegion": "string",
"egApproval": "string",
"smsId": "string",
"dateComSmsId": "2017-01-12T07:13:19.717Z",
"egRemarks": "string",
"dateComGbuTag": "2017-01-12T07:13:19.717Z",
"sgRemarks": "string",
"taggingDetailsIdFk": 0,
"profiling": "string",
"dateCompletedGbuTag": "2017-01-12T07:13:19.717Z",
"gbuTagidFk": 0,
"longitude": 0,
"latitude": 0,
"numberOfEmployees": "string",
"tier": "string",
"industryTypeIdFk": 0,
"gbuMigration": "string",
"finalReco": "string",
"motherAccountId": 0,
"remarksFindings": "string",
"finalCompanyName": "string",
"accountCategoryIdFk": 0,
"sourceLeadIdFk": 0,
"baluarte": true,
"vertical": true,
"affiliate": true,
"branch": true,
"profiled": true
};
$.ajax({
type : 'POST',
url : url,
data : data,
success : alert('success')
});
console.log(data);
});
Chrome Tools开发者网络
常规
Request URL:http://192.168.1.126/lms-dev-noel/proxy.php?endpoint=account/leads/&rt=POST
Request Method:POST
Status Code:200 OK
Remote Address:192.168.1.126:80
Response Headers
view source
Access-Control-Allow-Origin:*
Connection:Keep-Alive
Content-Length:841
Content-Type:text/json
Date:Fri, 13 Jan 2017 02:52:16 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.7 (Ubuntu)
X-Powered-By:PHP/5.5.9-1ubuntu4.20
请求标题
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:934
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:__atuvc=3%7C51%2C1%7C1; SESSfb2f81a034926ea9559505bfadcce9a3=RcchjmRlh0jHPn4LuTES7YMkAHnKbsotRdHIo2Gtbqw; SESS2b177789a8b29da818933ad27543b7ad=zjWnvbr75ZMuZMnwVXCb6PGkRzg8eTP0C2dCeaBZ9Sw; _ga=GA1.1.1071659288.1482134344
Host:192.168.1.126
Origin:http://192.168.1.126
Referer:http://192.168.1.126/lms-dev-noel/site/
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
X-Requested-With:XMLHttpRequest
查询字符串参数
endpoint:account/leads/
rt:POST
Form Data
view source
view URL encoded
isProfiled:true
isBaluarte:0
isVertical:0
isAffiliate:0
isBranch:0
id:0
dateRequested:2017-01-12T07:13:19.717Z
accountIdFk:0
accountLeadId:1285689131
accountName:Noel String
assignedEmployeeIdFk:0
unitNumber:string
floorNumber:string
buildingName:string
streetNumber:string
streetName:string
barangayIdFk:0
cityTownIdFk:0
provinceIdFk:0
regionIdFk:0
leadsIdentifier:string
salesRegion:string
egApproval:string
smsId:string
dateComSmsId:2017-01-12T07:13:19.717Z
egRemarks:string
dateComGbuTag:2017-01-12T07:13:19.717Z
sgRemarks:string
taggingDetailsIdFk:0
profiling:string
dateCompletedGbuTag:2017-01-12T07:13:19.717Z
gbuTagidFk:0
longitude:0
latitude:0
numberOfEmployees:string
tier:string
industryTypeIdFk:0
gbuMigration:string
finalReco:string
motherAccountId:0
remarksFindings:string
finalCompanyName:string
accountCategoryIdFk:0
sourceLeadIdFk:0
baluarte:true
vertical:true
affiliate:true
branch:true
profiled:true
答案 0 :(得分:3)
您需要使用JSON.stringify()
。
JSON.stringify函数将Javascript对象转换为JSON文本并将其存储在字符串中。
在服务器端,您必须parse
字符串。
参见参考here
app.on('addLead', function(event) {
var proxy = app.get('proxy');
var endpoint = 'account/leads';
var rt = 'POST';
var url = proxy+'?endpoint'+endpoint+'&rt='+rt;
data: JSON.stringify({
"id": 10,
"dateRequested": "2017-01-12",
"accountIdFk": 0,
"accountLeadId": 1305981891,
"accountName": "Noel String",
"assignedEmployeeIdFk": 10007901,
"unitNumber": "23",
"floorNumber": "12",
"buildingName": "Emerald",
"streetNumber": "123457",
"streetName": "Est Street",
"barangayIdFk": 0,
"cityTownIdFk": 0,
"provinceIdFk": 0,
"regionIdFk": 0,
"leadsIdentifier": "string",
"salesRegion": "string",
"egApproval": "string",
"smsId": "string",
"dateComSmsId": "2017-01-09",
"egRemarks": "string",
"dateComGbuTag": "2017-01-09",
"sgRemarks": "string",
}),
});