3

我想写一个例子来将数据从一个页面传递到另一个页面,但是有些事情不正确。

图1:首页输入一些文字显示在下一页。

index.mustache 文件

{{% handler:PerfectHandler}}<!DOCTYPE html>
<html lang="en">
<head>
    <title>{{title}}</title>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>
<h3>Hello!</h3>

<form method="POST"  action="welcome" id="sendText">
<input type="text" value="" placeholder="sometext" id="sometext" required />
<button>Submit</button>
</form>


<script>
$(document).ready(function() {
    $('#sendText').submit(function() {
    var sometext = $('#sometext').val()



    if (!sometext) {
        alert('Please enter some text!')
    } else {

    $.ajax({
        url: "welcome",
        type: "post",
        data: {'sometext':sometext},
        success: function(data, textStatus, jqXHR) {
        console.log('**data sent via ajax.**');
        console.log(data);
        document.location = '/welcome'
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.error("The following error occurred: "+ textStatus, errorThrown);
        }
    });
    }
    return false
})
});    
</script>

处理程序类 // // PerfectHandlers.swift //

import PerfectLib

    // This is the function which all Perfect Server modules must expose.
// The system will load the module and call this function.
// In here, register any handlers or perform any one-time tasks.
public func PerfectServerModuleInit() {

    // Register our handler class with the PageHandlerRegistry.
    // The name "PerfectHandler", which we supply here, is used within a mustache template to associate the template with the handler.


    PageHandlerRegistry.addPageHandler("PerfectHandler") {

        // This closure is called in order to create the handler object.
        // It is called once for each relevant request.
        // The supplied WebResponse object can be used to tailor the return value.
        // However, all request processing should take place in the `valuesForResponse` function.
        (r:WebResponse) -> PageHandler in

        return PerfectHandler()
    }


    // This handler takes the new user information and puts it in the database.
    PageHandlerRegistry.addPageHandler("WelcomeHandler") {
        return WelcomeHandler()
        }

}

// Handler class
// When referenced in a mustache template, this class will be instantiated to handle the request
// and provide a set of values which will be used to complete the template.
class PerfectHandler: PageHandler { // all template handlers must inherit from PageHandler

// This is the function which all handlers must impliment.
// It is called by the system to allow the handler to return the set of values which will be used when populating the template.
// - parameter context: The MustacheEvaluationContext which provides access to the WebRequest containing all the information pertaining to the request
// - parameter collector: The MustacheEvaluationOutputCollector which can be used to adjust the template output. For example a `defaultEncodingFunc` could be installed to change how outgoing values are encoded.
func valuesForResponse(context: MustacheEvaluationContext, collector: MustacheEvaluationOutputCollector) throws -> MustacheEvaluationContext.MapType {

    // The dictionary which we will return
    var values = [String:Any]()

    values["title"] = "Perfect Project Template"

    // Return the values
    // These will be used to populate the template
    return values
}

}

欢迎.mustache 文件

{{% handler:WelcomeHandler}}<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Message: {{text}}</h1>
</body>
</html>`

WelcomeHandler.swift 文件

import PerfectLib

func valuesForResponse(context: MustacheEvaluationContext, collector: MustacheEvaluationOutputCollector) throws -> MustacheEvaluationContext.MapType {

    // The dictionary which we will return
    var values = MustacheEvaluationContext.MapType()


    if let _ = context.webResponse, let request = context.webRequest {


        if request.requestMethod() == "POST" {

            if let text = request.param("sometext") {
                print("\(text) = sometext")
                values["text"] = text
            }

        }
}

     return values
    }
}

在我添加一些文本并按下提交后,如上图 1 所示,我的浏览器显示不会在浏览器中显示消息,而是在控制台中显示,如图 2 所示。

图2:按下提交按钮后的输出。

4

0 回答 0