我试图将一些表单数据作为JSON对象发送到 Force.com上的示例应用程序。我使用jQuery获取表单数据,并将其POST到我的服务器上的PHP文件,然后再将其发送到上面链接的示例应用程序。但是,我从示例应用程序获得的响应告诉我,在某些方面我犯了一些错误。
与示例Force.com应用程序通信的PHP文件:
客户端jQuery代码,将表单数据提交到PHP文件:
我从Force.com应用程序中得到的响应是:
这是 PHP 代码中
通用应用程序只需获得一个带有正确字段的JSON对象即可。当我通过REST客户端提交以下内容时,它按预期工作(为了易读性添加了换行符):
<?php
$url = 'https://cmsamp.secure.force.com/GenericApp/services/apexrest/GenericApp';
$data = $_POST[ 'data'];
$options = array('http' => array('method' => 'POST','content' => http_build_query($data)));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
?>
客户端jQuery代码,将表单数据提交到PHP文件:
var sample_form_data = {"attributes":{"type":"Generic_App__c"},"Application_Type__c":"iPay","Return_Email__c":"lsmith@cmsrep.com","Name":"Bus Test","ACHRejectFee__c":"123456789","ApplicationDate__c":"2000-01-01","BusinessPhone__c":"(555) 123-4567","Email__c":"thetest@testemail.com","InternetPercentage2__c":"0","MailingState__c":"CA","MOTO7__c":"true","NumOfLocations__c":"15"};
$.post( url, { data: JSON.stringify( sample_form_data ) }, function( result ) {
console.log( result );
});
我从Force.com应用程序中得到的响应是:
"No content to map to Object due to end of inputInsert failed.
First exception on row 0;
first error: REQUIRED_FIELD_MISSING,
Required fields are missing: [Name]: [Name]"
期望的“成功”响应:
"Success:
Generic App Object: Bus Test; was successfully created and inserted"
这是 PHP 代码中
var_dump($data)
的输出结果(为了易读性添加了换行符):
string(405)
"{\"attributes\":
{\"type\":\"Generic_App__c\"},
\"Application_Type__c\":\"iPay\",
\"Return_Email__c\":\"lsmith@cmsrep.com\",
\"Name\":\"Bus Test\",
\"ACHRejectFee__c\":\"123456789\",
\"ApplicationDate__c\":\"2000-01-01\",
\"BusinessPhone__c\":\"(555) 123-4567\",
\"Email__c\":\"thetest@testemail.com\",
\"InternetPercentage2__c\":\"0\",
\"MailingState__c\":\"CA\",
\"MOTO7__c\":\"true\",
\"NumOfLocations__c\":\"15\"
}"
通用应用程序只需获得一个带有正确字段的JSON对象即可。当我通过REST客户端提交以下内容时,它按预期工作(为了易读性添加了换行符):
{"attributes":
{"type":"Generic_App__c"},
"Application_Type__c":"iPay",
"Return_Email__c":"test@example.org",
"Name":"Bus Test",
"ACHRejectFee__c":"123456789",
"ApplicationDate__c":"2000-01-01",
"BusinessPhone__c":"(555) 123-4567",
"Email__c":"thetest@testemail.com",
"InternetPercentage2__c":"0",
"MailingState__c":"CA",
"MOTO7__c":"true",
"NumOfLocations__c":"15"}
有人对如何解决这个问题有什么想法吗?
var_dump($data);
。此外,是否有关于示例应用程序期望的文档?实际处理程序的响应消息不足以确定它期望的格式。 - Patrick M