【发布时间】:2018-05-18 05:00:08
【问题描述】:
这是我的rest.php 文件
<?php
chdir('../../..');
require_once 'SugarWebServiceImplv4_1_custom.php';
$webservice_path = 'service/core/SugarRestService.php';
$webservice_class = 'SugarRestService';
$webservice_impl_class = 'SugarWebServiceImplv4_1_custom';
$registry_path = 'custom/service/v4_1_custom/registry.php';
$registry_class = 'registry_v4_1_custom';
$location = 'custom/service/v4_1_custom/rest.php';
require_once 'service/core/webservice.php';
这是我编写自定义方法的 SugarWebServiceImplv4_1_custom.php 文件
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
if(!defined('sugarEntry')){
define('sugarEntry', true);
}
require_once 'service/v4_1/SugarWebServiceImplv4_1.php';
class SugarWebServiceImplv4_1_custom extends SugarWebServiceImplv4_1
{
public function custom_test($username)
{
$arr = array ('a'=>$username,'b'=>22,'c'=>32,'d'=>44,'e'=>55);
return json_encode($arr);
die;
}
}
这是我注册自定义方法的 registry.php 文件
<?php
require_once 'service/v4_1/registry.php';
class registry_v4_1_custom extends registry_v4_1
{
protected function registerFunction()
{
parent::registerFunction();
$this->serviceClass->registerFunction('custom_test',
array(
'username'=>'xsd:string),
array(
'return'=>'tns:get_array')
);
}
}
问题是当我像这样通过get方法传递数据时
{"用户名":"一些 用户名"}
我得到了结果,但我不知道如何通过 IOS 应用程序通过 post 方法传递它。我试图通过它,但我没有在用户名中得到任何东西。 我也通过 curl 检查了响应,它正在使用 curl,但我必须将它连接到 IOS。 帮助将不胜感激
实际上,我们正在使用 Angular 5 和 Ionic 3 为 IOS 构建一个混合应用程序 这是代码 auth-services.ts
public login(credentials){
if(credentials.username === null || credentials.password === null){
return Observable.throw("Please enter credentials");
} else {
this.username1 = credentials.username;
this.password1 = credentials.password;
return Observable.create(observer =>{
// At this point make a request to your backend to make a real check!
this.method1 = "custom_test";
this.inputType = "JSON";
this.responseType = "JSON";
this.encryptionValue = "PLAIN";
this.bodyData = {}; //get method calling
console.log(JSON.stringify(this.bodyData));
//Sending the Username and Password to the Web Server for authentication. Change the URL Get the response message
this.servicesProvider.restApi("post","http://exmaple.com/custom/service/v4_1_custom/rest.php",this.bodyData).then(
(res) => {
console.log("Response stringify :",JSON.stringify(res));
console.log("Response parse :", res);
console.log("Status :",res.status);
this.response = res.status; //TODO: Replace res.username with res.message as we have to check for user exist or not.
if(this.response == "success out") {
this.success = true;
this.storage.set("status",this.response); //Username value stored in localstorage
this.currentUser = new User('Simon', 'saimon@devdactic.com');
observer.next(this.success);
observer.complete();
} else {
this.success = false;
observer.next(this.success);
observer.complete();
}
}
);
}
这是 services.ts 文件。这是一个常用的rest api文件,用于发送rest api请求。
restApi(method,url,data) {
console.log("inside restApi");
switch(method) {
case 'post' : {
console.log("Inside Post Method");
/*
return this.httpClient.post(url,data)
.subscribe(
(res:any) => {
console.log("POST response below");
console.log(res.username);
this.responseData = JSON.stringify(res);
console.log("ResponseData Value");
console.log(this.responseData);
return this.responseData;
}); */
let headers = new Headers({'content-type':'application/json'});
let options = new RequestOptions({ headers:this.headers });
this.responseFromFunction = this.http.post(url,data).toPromise()
.then(this.extractData)
.catch(this.handleError);
break;
}
case 'get' : {
console.log("Inside Get Method");
let headers = new Headers({'content-type':'application/json'});
let options = new RequestOptions({ headers:this.headers });
this.responseFromFunction = this.http.get(url, options).toPromise()
.then(this.extractData)
.catch(this.handleError);
break;
}
case 'put' : {
console.log("Inside Put Method");
this.responseFromFunction = this.httpClient.put(url,data)
.subscribe((res:any) => {
console.log(res);
});
break;
}
case 'delete' : {
console.log("Inside Delete Method");
this.responseFromFunction = this.httpClient.delete(url)
.subscribe((res:any) => {
console.log(res);
});
break;
}
default : {
this.responseFromFunction = {"message":"error"};
console.log("Unknow Method Entered. Or write method in small lowercase only");
// return "Invalid Method";
}
}
console.log("Outside switch case");
console.log(this.responseFromFunction);
return this.responseFromFunction;
}
private extractData(res: Response) {
// console.log("Resp :", res.json());
// console.log("Stringy :", JSON.stringify(res));
return res.json();
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
我不知道如何在 rest_data 中传递用户名
【问题讨论】:
-
你的 iOS 代码在哪里?
-
我刚刚编辑了我的代码,请检查。
-
是的,它正在使用 get 方法,但不适用于 post。我们使用 POSTMAN 对其进行了检查,但使用 post 也无法正常工作
-
请查看邮递员截图
-
我收到了回复,但是因为我已经通过帖子传递了用户名的值,但我没有得到回复。