AJAX请求,使用params重定向

时间:2017-06-07 18:11:06

标签: javascript jquery ajax

public class AbstractAction{
    // not relevant for global action but let's put that aside for the example
    public abstract void endTurn();
}

public class GlobalAction{
    // ...
}

public class MoneyAction{

    enum Action{
        PLACE_BET, PLACE_MAX_BET, SPLIT_BET, ...;
    }

    private MoneyAction.Action action;
    // ...
}

public class CardAction{
    // ...
}

这是我的ajax请求,它以JSON的形式发送,并返回一个对象

即。 search = {firstName:km,lastname:b}

但是,ajax没有为重定向中的参数设置内容类型为JSON。它仍然作为对象发送。 JSON stringify不起作用。有没有办法在成功/重定向功能中将内容类型设置为JSON?

JSON.stringify只是将对象解码为

@ServerEndPoint(value = ..., encoders = {...}, decoders = {...})
public class ServerEndPoint{

    @OnOpen
    public void onOpen(Session session){
        // ...
    }

    @OnClose
    public void onClose(Session session){
        // ...
    }

    @OnMessage
    public void onMessage(Session session, AbstractAction action){

        // I'm checking the class here but you
        // can use different check such as a 
        // specific attribute 

        if(action instanceof GlobalAction){
            // do some stuff
        }

        else if (action instanceof CardAction){
            // do some stuff
        }

        else if (action instance of MoneyAction){
            MoneyAction moneyAction = (MoneyAction) action;
            switch(moneyAction.getAction()){
                case PLACE_BET:
                    double betValue = moneyAction.getValue();
                    // do some stuff here
                    break;
                case SPLIT_BET:
                    doSomeVeryComplexStuff(moneyAction);
                    break;
            }
        }

    }


    private void doSomeVeryComplexStuff(MoneyAction moneyAction){
        // ... do something very complex ...
    }

}

2 个答案:

答案 0 :(得分:0)

您可以使用$.param

var qs = $.param(data.search);
window.location = 'http://localhost:9000/#/home.html?' + qs;

答案 1 :(得分:-1)

这可能是因为它试图将一个对象发送到你的window.location部分。  您可能需要的是:

 window.location = ('http://localhost:9000/#/home.html?firstName=' +  data.search.firstName + "&lastname=" + data.search.lastname)