gae运行平稳但无法使用html和client.js检索数据

时间:2022-12-17 23:11:03

I am running into an issue where all google app endpoints are running smoothly. No issue at getting, updating or deleting entries while using the API explorer.

我遇到了一个问题,所有谷歌应用程序端点都运行顺利。使用API​​资源管理器时获取,更新或删除条目没有问题。

I am running into issue when trying to mesh all this together in an html file. Updates and deletes work fine... Only get/list methods are returning no record.

尝试在html文件中将所有这些拼接在一起时,我遇到了问题。更新和删除工作正常...只有get / list方法没有返回记录。

Any idea what the issue is? What is best to troubleshoot this kind of issue?

知道问题是什么吗?什么最好解决这类问题?

Thanks.

谢谢。

test.java

test.java

import com.googlecode.objectify.annotation.*;

@Entity
@Index
public class Test {

@Id Long number;
Long number2;

public Test(){

}

public Test(Long number, Long number2){
    this.number = number;
    this.number2 = number2;
}
public Test(Long number2){
    this.number2 = number2;
}

public Long getNumber2() {
    return number2;
}

public void setNumber2(Long number2) {
    this.number2 = number2;
}

public Long getNumber() {
    return number;
}

public void setNumber(Long number) {
    this.number = number;
}

}

}

test.html

的test.html

<html>
<head>
<title></title>
</head>
<body>

<div>
    <form>
        <div>
            <input type="text" id="insert" value="2"><br><br>
            <button type="submit" id="getTestButton" onclick="getTest()" disabled>Get</button>
        </div>

    </form>

    <p id="result"></p>
    <hr>
</div>
<!--
 Load the Google APIs Client Library for JavaScript
 More info here : https://developers.google.com/api-client-    library/javascript/reference/referencedocs
-->
<script type="text/javascript">
function init() {
    gapi.client.load('testEndpoint', 'v1', enableButton, 'http://localhost:8080/_ah/api');

    document.getElementById('getTestButton').onclick = function() {
        getTest();
    }
}

function enableButton() {
    console.log("enabling button");
    getTestButton.disabled = false;
}

function getTest() {
    console.log("entering getTest function");

    var features = {};
    features.number2 = document.getElementById("insert").value;

    console.log(features);

    var req = gapi.client.testEndpoint.getTest(features);

    req.execute(function(data) {
        console.log(data);
        document.getElementById("result").innerHTML = data.number + " " + data.number2;
    });
}
</script>

<script src="https://apis.google.com/js/client.js?onload=init"></script>
</body>
</html>

TestEndpoint.java

TestEndpoint.java

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;

import java.util.logging.Logger;

import javax.inject.Named;
import com.googlecode.objectify.cmd.Query;

import static com.Backend.OfyService.ofy;

/** An endpoint class we are exposing */
@Api(name = "testEndpoint", version = "v1", namespace = @ApiNamespace(ownerDomain =    "Backend.com", ownerName = "Backend.com", packagePath=""))
public class TestEndpoint {

    // Make sure to add this endpoint to your web.xml file if this is a web application.

    private static final Logger LOG = Logger.getLogger(TestEndpoint.class.getName());

    /**
     * This method gets the <code>Test</code> object associated with the specified    <code>id</code>.
     * @param id The id of the object to be returned.
     * @return The <code>Test</code> associated with <code>id</code>.
     */
    @ApiMethod(name = "getTest")
    public Test getTest(@Named("number2") Long number2) {
        // Implement this function
        LOG.info("***GetTest***NUMBER=" + number2);
        Test t = new Test(1L, 2L);

        //t = ofy().load().type(Test.class).filter("number2", number2).first().now();

        LOG.info("t.getNumber()==> " + t.getNumber().toString());
        LOG.info("Calling getTest method");

        return t;
    }
}

1 个解决方案

#1


0  

Seems legit to me. To isolate the cause of the error, I would suggest to do the following :

似乎对我来说是合法的。为了找出错误原因,我建议您执行以下操作:

  1. Test your getTest endpoint method through the API explorer, located at http://yourAppAddress/_ah/api/explorer . If it works you know the issue is in your client code.

    通过位于http:// yourAppAddress / _ah / api / explorer的API资源管理器测试您的getTest端点方法。如果它有效,您就会知道问题出在您的客户端代码中。

  2. If it did not work, then the issue is either with the code inside getTest or with the way you use endpoints (or with endpoints itself). To check this, create a pure Java servlet that will call the getTest() method directly, and log the result. If it works you know the issue is either on the way you use endpoints or on endpoints itself.

    如果它不起作用,那么问题是使用getTest中的代码或使用端点(或使用端点本身)的方式。要检查这一点,请创建一个直接调用getTest()方法的纯Java servlet,并记录结果。如果它有效,您就会知道问题在于您使用端点的方式还是在端点本身上。

Sorry I don't have an immediate answer to your question.

对不起,我没有立即回答你的问题。

EDIT : Since you tell me that the API Explorer test works, I checked your javascript code and there's something weird : you try to get resp.items.length in the callback. But your endpoint method only returns a Test entity, so there are no items attribute.

编辑:既然你告诉我API Explorer测试工作,我检查了你的javascript代码,有一些奇怪的东西:你试图在回调中得到resp.items.length。但是您的端点方法只返回一个Test实体,因此没有items属性。

Can you try this code ?

你能试试这段代码吗?

gapi.client.testEndpoint.getTest(requestData).execute(function(resp) {
       console.log(resp);
});

EDIT 2 : So you've found the cause of the issue by yourself : you cannot call the endpoints method before the API client is loaded. Since your method is called by an onClick event, probably on a button, you must do the following :

编辑2:所以你自己找到了问题的原因:在加载API客户端之前,你不能调用端点方法。由于您的方法是由onClick事件调用的,可能是在按钮上,您必须执行以下操作:

  • By default, disable the button
  • 默认情况下,禁用该按钮
  • In the gapi.client.load 's callback, enable the button
  • 在gapi.client.load的回调中,启用按钮

That way you will be sure to only click on the button when the Google service is initialized.

这样,您就可以确保只在初始化Google服务时点击按钮。

EDIT 3 : It is actually a problem in your html. You have a submit button, which makes Chrome reload the page. Every time you click the button the page reloads and you never see the result of your endpoint query.

编辑3:它实际上是你的HTML中的一个问题。您有一个提交按钮,这会让Chrome重新加载页面。每次单击该按钮时,页面都会重新加载,您永远不会看到终端查询的结果。

This HTML file works for me on the dev server :

这个HTML文件在dev服务器上适用于我:

<html>
<head>
    <title></title>
</head>
<body>

<div>

    <div>
        <input type="text" id="insert" value="2"><br><br>
        <button id="getTestButton" onclick="getTest()" disabled>Get</button>
    </div>


    <p id="result"></p>
    <hr>
</div>
<!--
 Load the Google APIs Client Library for JavaScript
 More info here : https://developers.google.com/api-client-    library/javascript/reference/referencedocs
-->
<script type="text/javascript">
    function init() {
        gapi.client.load('testEndpoint', 'v1', enableButton, '/_ah/api');

        document.getElementById('getTestButton').onclick = function () {
            getTest();
        }
    }

    function enableButton() {
        console.log("enabling button");
        getTestButton.disabled = false;
    }

    function getTest() {
        console.log("entering getTest function");

        var features = {};
        features.number2 = document.getElementById("insert").value;

        console.log(features);

        var req = gapi.client.testEndpoint.getTest(features);

        req.execute(function (data) {
            console.log(data);
            document.getElementById("result").innerHTML = data.number + " " + data.number2;
        });
    }
</script>

<script src="https://apis.google.com/js/client.js?onload=init"></script>
</body>
</html>

#1


0  

Seems legit to me. To isolate the cause of the error, I would suggest to do the following :

似乎对我来说是合法的。为了找出错误原因,我建议您执行以下操作:

  1. Test your getTest endpoint method through the API explorer, located at http://yourAppAddress/_ah/api/explorer . If it works you know the issue is in your client code.

    通过位于http:// yourAppAddress / _ah / api / explorer的API资源管理器测试您的getTest端点方法。如果它有效,您就会知道问题出在您的客户端代码中。

  2. If it did not work, then the issue is either with the code inside getTest or with the way you use endpoints (or with endpoints itself). To check this, create a pure Java servlet that will call the getTest() method directly, and log the result. If it works you know the issue is either on the way you use endpoints or on endpoints itself.

    如果它不起作用,那么问题是使用getTest中的代码或使用端点(或使用端点本身)的方式。要检查这一点,请创建一个直接调用getTest()方法的纯Java servlet,并记录结果。如果它有效,您就会知道问题在于您使用端点的方式还是在端点本身上。

Sorry I don't have an immediate answer to your question.

对不起,我没有立即回答你的问题。

EDIT : Since you tell me that the API Explorer test works, I checked your javascript code and there's something weird : you try to get resp.items.length in the callback. But your endpoint method only returns a Test entity, so there are no items attribute.

编辑:既然你告诉我API Explorer测试工作,我检查了你的javascript代码,有一些奇怪的东西:你试图在回调中得到resp.items.length。但是您的端点方法只返回一个Test实体,因此没有items属性。

Can you try this code ?

你能试试这段代码吗?

gapi.client.testEndpoint.getTest(requestData).execute(function(resp) {
       console.log(resp);
});

EDIT 2 : So you've found the cause of the issue by yourself : you cannot call the endpoints method before the API client is loaded. Since your method is called by an onClick event, probably on a button, you must do the following :

编辑2:所以你自己找到了问题的原因:在加载API客户端之前,你不能调用端点方法。由于您的方法是由onClick事件调用的,可能是在按钮上,您必须执行以下操作:

  • By default, disable the button
  • 默认情况下,禁用该按钮
  • In the gapi.client.load 's callback, enable the button
  • 在gapi.client.load的回调中,启用按钮

That way you will be sure to only click on the button when the Google service is initialized.

这样,您就可以确保只在初始化Google服务时点击按钮。

EDIT 3 : It is actually a problem in your html. You have a submit button, which makes Chrome reload the page. Every time you click the button the page reloads and you never see the result of your endpoint query.

编辑3:它实际上是你的HTML中的一个问题。您有一个提交按钮,这会让Chrome重新加载页面。每次单击该按钮时,页面都会重新加载,您永远不会看到终端查询的结果。

This HTML file works for me on the dev server :

这个HTML文件在dev服务器上适用于我:

<html>
<head>
    <title></title>
</head>
<body>

<div>

    <div>
        <input type="text" id="insert" value="2"><br><br>
        <button id="getTestButton" onclick="getTest()" disabled>Get</button>
    </div>


    <p id="result"></p>
    <hr>
</div>
<!--
 Load the Google APIs Client Library for JavaScript
 More info here : https://developers.google.com/api-client-    library/javascript/reference/referencedocs
-->
<script type="text/javascript">
    function init() {
        gapi.client.load('testEndpoint', 'v1', enableButton, '/_ah/api');

        document.getElementById('getTestButton').onclick = function () {
            getTest();
        }
    }

    function enableButton() {
        console.log("enabling button");
        getTestButton.disabled = false;
    }

    function getTest() {
        console.log("entering getTest function");

        var features = {};
        features.number2 = document.getElementById("insert").value;

        console.log(features);

        var req = gapi.client.testEndpoint.getTest(features);

        req.execute(function (data) {
            console.log(data);
            document.getElementById("result").innerHTML = data.number + " " + data.number2;
        });
    }
</script>

<script src="https://apis.google.com/js/client.js?onload=init"></script>
</body>
</html>