2017-10-25 7 views
1

APIを呼び出して文字列を返しますが、エラーが発生しましたが、エラーが発生します。 未知の型エラー:未定義の 'sayHello'プロパティを読み取れません greetGenerically (hello.js:62) HTMLInputElement.btn.onclickで(hello.js:44)未知型エラー:greetGenericallyで未定義のプロパティ 'sayHello'を読み取ることができません。

私は、私は私のクラスまたは何のエラーを引き起こしている可能性がありますの戻り値の型を変更する必要があるかどうかを知りたいです。

メインJSファイル:

function init() { 

    // You need to pass the root path when you load your API 
    // otherwise calls to execute the API run into a problem 

    // rootpath will evaulate to either of these, depending on where 
the app is running: 
    // //localhost:8080/_ah/api 
    // //your-app-id/_ah/api 

    var rootpath = "//" + window.location.host + "/_ah/api"; 

    // Load the helloworldendpoints API 
     // If loading completes successfully, call loadCallback function 
    gapi.client.load('helloworldendpoints', 'v1', loadCallback, 
rootpath); 
} 

/* 
* When helloworldendpoints API has loaded, this callback is called. 
* 
    * We need to wait until the helloworldendpoints API has loaded to 
    * enable the actions for the buttons in index.html, 
* because the buttons call functions in the helloworldendpoints API 
*/ 
function loadCallback() { 
    // Enable the button actions 
    enableButtons(); 
} 

function enableButtons() { 
    // Set the onclick action for the first button 
    btn = document.getElementById("input_greet_generically"); 
    btn.onclick= function(){greetGenerically();}; 

    // Update the button label now that the button is active 
    btn.value="Click me for a generic greeting"; 

    // Set the onclick action for the second button 
    btn = document.getElementById("input_greet_by_name"); 
    btn.onclick=function(){greetByName();}; 

    // Update the button label now that the button is active 
    btn.value="Click me for a personal greeting"; 
} 

/* 
    * Execute a request to the sayHello() endpoints function 
    */ 
function greetGenerically() { 
// Construct the request for the sayHello() function 
var request = gapi.client.helloworldendpoints.sayHello(); 

// Execute the request. 
// On success, pass the response to sayHelloCallback() 
sayHelloCallback(request); 

}

/* 
    * Execute a request to the sayHelloByName() endpoints function. 
    * Illustrates calling an endpoints function that takes an argument. 
*/ 
function greetByName() { 
// Get the name from the name_field element 
var name = document.getElementById("name_field").value; 

// Call the sayHelloByName() function. 
// It takes one argument "name" 
// On success, pass the response to sayHelloCallback() 
var request = gapi.client.helloworldendpoints.sayHelloByName({'name': name}); 
request.execute(sayHelloCallback); 

}

// Process the JSON response 
// In this case, just show an alert dialog box 
// displaying the value of the message field in the response 
function sayHelloCallback (response) { 
alert(response.message);  

}

私のエンドポイント・ファイル:

package com.google.training.helloworld; 

import com.google.api.server.:.config.Api; 
import com.google.api.server.spi.config.ApiMethod; 
import com.google.api.server.spi.config.ApiMethod.HttpMethod; 
import com.google.api.server.spi.config.Named; 

/** 
* Defines endpoint functions APIs. 
*/ 
@Api(name = "helloworldendpoints", version = "v1", 
    scopes = {Constants.EMAIL_SCOPE }, 
    clientIds = {Constants.WEB_CLIENT_ID, 
    Constants.API_EXPLORER_CLIENT_ID }, 
    description = "API for hello world endpoints.") 

public class HelloWorldEndpoints { 

    // Declare this method as a method available externally through 
Endpoints 
@ApiMethod(name = "sayHello", path = "sayHello", 
     httpMethod = HttpMethod.GET) 

public HelloClass sayHello() { 
    return new HelloClass(); 
} 

// Declare this method as a method available externally through Endpoints 
@ApiMethod(name = "sayHelloByName", path = "sayHelloByName", 
     httpMethod = HttpMethod.GET) 

public HelloClass sayHelloByName (@Named("name") String name) { 
    return new HelloClass(name); 
} 

}

こんにちは私のクラスファイル:

package com.google.training.helloworld; 

public class HelloClass { 
    public String message = "Hello World"; 

    public HelloClass() { 

    } 

    public HelloClass (String name) { 
    this.message = "Hello " + name + "!"; 
    } 

    public String getMessage() { 
    return message; 
    } 
} 

答えて

0

コメントをするたかったが、そうするだけの50議員の下で持っています。

htmlファイルに<script src="https://apis.google.com/js/client.js?onload=init"></script>がありますか?

+0

はい、あります。それがエラーに影響していますか? – Jack

+0

ちょうど 'gapi'としてチェックされるのは、js – jERCle

+0

で定義されているようです.CPD200 Hello Endpointsコースをやっていますか?もしそうなら、ソリューションとコードを比較しましたか? – jERCle

関連する問題