I created an API backend:
我创建了一个API后端:
@ApiMethod(name = "create", path = "properties", httpMethod = HttpMethod.POST)
public void create(RealEstatePropertyAPI property, User user)
throws Exception {
}
with the following data model:
使用以下数据模型:
public class RealEstatePropertyAPI {
private Long id;
private String name;
private AddressAPI address;
public RealEstatePropertyAPI() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AddressAPI getAddress() {
return address;
}
public void setAddress(AddressAPI address) {
this.address = address;
}
}
public class AddressAPI {
private Long id;
private String street;
private String city;
private String state;
private String zip;
private String country;
public AddressAPI() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
In API Explorer
the following request works:
在API Explorer中,以下请求有效:
POST http://localhost:8888/_ah/api/realestate/v1/properties
Content-Type: application/json
X-JavaScript-User-Agent: Google APIs Explorer
{
"name": "test",
"address": {
"city": "DC"
}
}
But when using an Angular JS
client, the RealEstatePropertyAPI
instance is created but the fields are not populated (all null
). The request is:
但是在使用Angular JS客户端时,会创建RealEstatePropertyAPI实例,但不填充字段(全部为空)。请求是:
[{"jsonrpc":"2.0","id":"gapiRpc","method":"realestate.create","params":{"resource":"{\"name\": \"Test\",\"address\": { \"street\": \"White House\"}}"},"apiVersion":"v1"}]
The JS call:
JS电话:
var x = '{"name": "Test","address": { "street": "White House"}}';
gapi.client.realestate.create({"resource": x}).execute(function(resp) {
console.log(resp);
});
2 个解决方案
#1
0
I ran into this same issue but never figured out how to solve it. I ended up not using the JS library for my insert method. Instead, I created the call like this:
我遇到了同样的问题,但从来没有弄清楚如何解决它。我最终没有将JS库用于我的insert方法。相反,我创建了这样的调用:
$http({
'url': API_ROOT + '/mobilebackend/v1/CloudEntities/insert/private_Note',
'dataType': 'json',
'method': 'POST',
'data': JSON.stringify(data),
'headers': {
'Content-Type': 'application/json; charset=utf-8'
}
}).success(function(resp) {
next(resp);
}).error(function(error) {
next(error);
});
I wish I had a better alternative, but this is all I've come up with so far.
我希望我有一个更好的选择,但这是我迄今为止所提出的全部内容。
#2
0
I actually managed to make it work. property
is a $scope
variable used in the form. You have to convert the property
into JSON which is a String
, then the JSON String
must be convert back to an object.
我实际上设法让它工作。 property是表单中使用的$ scope变量。您必须将属性转换为JSON(即String),然后必须将JSON String转换回对象。
gapi.client.realestate.create(angular.fromJson(angular.toJson(property))).execute(function(resp) {
console.log(resp);
});
Inputs from the form.
来自表单的输入。
<input type="text" id="inputName" placeholder="Real Estate Name" ng-model="property.name">
<input type="text" id="inputStreet" placeholder="Street Address" ng-model="property.address.street" name="uStreet" required>
#1
0
I ran into this same issue but never figured out how to solve it. I ended up not using the JS library for my insert method. Instead, I created the call like this:
我遇到了同样的问题,但从来没有弄清楚如何解决它。我最终没有将JS库用于我的insert方法。相反,我创建了这样的调用:
$http({
'url': API_ROOT + '/mobilebackend/v1/CloudEntities/insert/private_Note',
'dataType': 'json',
'method': 'POST',
'data': JSON.stringify(data),
'headers': {
'Content-Type': 'application/json; charset=utf-8'
}
}).success(function(resp) {
next(resp);
}).error(function(error) {
next(error);
});
I wish I had a better alternative, but this is all I've come up with so far.
我希望我有一个更好的选择,但这是我迄今为止所提出的全部内容。
#2
0
I actually managed to make it work. property
is a $scope
variable used in the form. You have to convert the property
into JSON which is a String
, then the JSON String
must be convert back to an object.
我实际上设法让它工作。 property是表单中使用的$ scope变量。您必须将属性转换为JSON(即String),然后必须将JSON String转换回对象。
gapi.client.realestate.create(angular.fromJson(angular.toJson(property))).execute(function(resp) {
console.log(resp);
});
Inputs from the form.
来自表单的输入。
<input type="text" id="inputName" placeholder="Real Estate Name" ng-model="property.name">
<input type="text" id="inputStreet" placeholder="Street Address" ng-model="property.address.street" name="uStreet" required>