脚本发送对象而不是数字

时间:2016-01-10 08:05:47

标签: javascript jquery

以下脚本发送对象而不是数字。我进入控制台:

  

GET   http://localhost:8080/new_prog_22/paginator.action?page=[object%20Object]   500(内部服务器错误)

我只需要发送hash变量,这是一个数字,因为我的链接看起来像

<a class="idPage" href="#4">4</a>

当我按下链接时,我会在浏览器中收到提醒通知An error occurred! ' + thrownError

此外,我不会在cosnsole中收到console.log("**** hash = "....

的输出

脚本:

function paginFunc() {

    $( function() {
        $(window).on('hashchange', refreshByHash);
        var hash = window.location.hash; 
        hash = hash.substring(1);
        console.log("**** hash = " + hash + " ****");
        refreshByHash( hash );
    });


    function refreshByHash( hash ) {
        $.ajax({
            url : 'paginator.action?page=' + hash, // action to be perform
            type : 'GET', //type of posting the data
            dataType : 'html',
            async: true,
            success : function(htmlData) {
                $('#paginator').html(htmlData);
            },
            error : function(xhr, ajaxOptions, thrownError) {
                alert('An error occurred! ' + thrownError);
            },
        });                   
    }

}

此代码有什么问题?

1 个答案:

答案 0 :(得分:2)

您正在使用函数内的paginFunc(简短版本),只有在调用函数时文档尚未就绪时才会执行。我建议你将此代码移到hashchange之外。此外,当window事件触发$(window).on('hashchange', function(event){ //... }); 时,第一个参数成为jQuery公开的事件数据:

.triggerHandler('hashchange')

您需要移动检查函数内部哈希的代码部分。如果您想像以前一样触发处理程序,请在.on()调用后添加$(function(){ $(window).on('hashchange', refreshByHash); }); function refreshByHash(){ var hash = window.location.hash.substring(1); $.ajax({ url: 'paginator.action?page=' + hash, type: 'GET', dataType: 'html', async: true, success: function(htmlData) { $('#paginator').html(htmlData); }, error: function(xhr, ajaxOptions, thrownError) { alert('An error occurred! ' + thrownError); }, }); }

hash

如果希望保留$(function(){ $(window).on('hashchange', function(event){ refreshByHash(window.location.hash.substring(1)); }); var hash = window.location.hash.substring(1); refreshByHash(hash); }); function refreshByHash(hash){ $.ajax({ url: 'paginator.action?page=' + hash, type: 'GET', dataType: 'html', async: true, success: function(htmlData) { $('#paginator').html(htmlData); }, error: function(xhr, ajaxOptions, thrownError) { alert('An error occurred! ' + thrownError); }, }); } 参数,则需要更改处理程序,但我认为您应坚持上述版本

Builder.load_string("""

<InputBar@BoxLayout>:
    orientation: 'horizontal'
    Label:
        text: 'some text'
    TextInput:
        text: 'input?'
    Label:
        text: 'some text2'
    TextInput:
        text: 'input2?'
<A>:
    orientation: 'vertical'
    Label:
        text: 'some title'
    InputBar:
        id: input1
    InputBar:
        id: input2
    InputBar:
        id: input3
    InputBar:
        id: input4
    Label:
        text: 'some title2'
    InputBar:
        id: input5
    InputBar:
        id: input6
    InputBar:
        id: input7
    InputBar:
        id: input8
    InputBar:
        id: input9
""")

class A(BoxLayout):
    pass


class VApp(App):

    def build(self):


        return A()


if __name__ == '__main__':
    VApp().run()