问题描述
我有一个 Cloud Endpoints
方法,如下所示:
// HTTP POST
@ApiMethod(name =hylyts.insert)
public Hylyt insertHylyt(@Named(url)字符串url,Hylyt hylyt,用户用户)
抛出OAuthRequestException {
log.info(试图保存hylyt'+ hylyt +'with id'+ hylyt.getId());
if(user == null)抛出新的OAuthRequestException(你的令牌在这里没有用。);
hylyt.setArticle(getArticleKey(url,user));
ofy()。save()。entity(hylyt);
返回hylyt;
}
我从 Javascript客户端库调用它
code>使用这个:
gapi.client.hylytit.hylyts.insert({PARAMS})。execute(callback );
现在,如果我构造
{PARAMS}
如(第二个示例)中所述,
{
'url':url,
'资源':{
'hylyt ':{
'contentType':'application / json',
'data':hylyt
}
}
}
我在端点中获得一个空对象(更不用说这个库的全部要点是使这些调用变得简单,这种结构清晰违反)。
{
'url':url,-bo / 19809245#19809245
'resource':hylyt
}
{
'url':url,
'id':hylyt .id
'text':hylyt.text
}
我脑海。我做这一切都错了吗?这是一个错误?这是否仅仅是因为 gapi
也在后台传递了 auth token
?
是的,我可以使用请求
语法,但是,如果它与使用 XHR
在纯JavaScript中?如果谷歌在文档中解释为什么事情正在发生,我不介意这种复杂性。但是,这些文档,换言之,就是说使用这些方法,auth,CORS和XHR魔法会在闭门造车后发生。
解决方案
该错误已解决。正确的语法是:
pre $ gap $.txt );
I have a Cloud Endpoints
method that looks like this:
//HTTP POST
@ApiMethod(name = "hylyts.insert")
public Hylyt insertHylyt(@Named("url") String url, Hylyt hylyt, User user)
throws OAuthRequestException{
log.info("Trying to save hylyt '"+hylyt+"' with id '"+hylyt.getId());
if (user== null) throw new OAuthRequestException("Your token is no good here.");
hylyt.setArticle(getArticleKey(url, user));
ofy().save().entity(hylyt);
return hylyt;
}
I call it from the Javascript Client Library
using this:
gapi.client.hylytit.hylyts.insert({PARAMS}).execute(callback);
Now, if I structure {PARAMS}
as suggested in the docs (second example),
{
'url': url,
'resource': {
'hylyt': {
'contentType': 'application/json',
'data': hylyt
}
}
}
I get a null object in the endpoint (not to mention that the whole point of this library is to make these calls simple, which this structure clearly violates).
When I structure {PARAMS}
as these answers suggest,
{
'url': url,
'resource': hylyt
}
I get a null object in the endpoint again. The correct syntax is this:
{
'url': url,
'id': hylyt.id
'text': hylyt.text
}
Which just blows my mind. Am I doing this all wrong? Is this a bug? Is it only happening because gapi
is also passing the auth token
in the background?
Yes, I could use the request
syntax instead, but, again, why even use the library if it's just as complex as making the XHR
s in pure javascript? I wouldn't mind the complexity if Google explained in the docs why things are happening. But the docs, paraphrased, just say use these methods and the auth, CORS, and XHR magic will happen behind closed doors.
The bug has been resolved. The correct syntax is
gapi.client.hylytit.hylyts.insert({url: url}, hylyt).execute(callback);
这篇关于使用@Named和未命名参数从Javascript客户端库调用没有意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!