1

这是我正在尝试的:

在客户端:

Ext.data.JsonP.request({
    url: "http://172.24.87.38:9090/DynamicWeb/hello-world",
    params: {
    },
    callback: function (result) {
        console.log(result);
        if (response.success === true) {
            Ext.Msg.alert('Link Shortened', response.result, Ext.emptyFn);
        } else {
            Ext.Msg.alert('Error', response.result, Ext.emptyFn);
        }
    }
});

在其他域(服务器端):

公共类 HelloWorldServlet 扩展 HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    /*resp.setContentType("text/html");

    PrintWriter out = resp.getWriter();
    out.print("Hello World from Servlet");
    out.flush();
    out.close();*/
    boolean jsonP=false;
    String cd=req.getParameter("callback");
    String n= "{data:Hello World from Servlet}";        
    if (cd!=null) {
        jsonP=true;
        resp.setContentType("text/javascript");
    } else {
        resp.setContentType("application/x-json");
    }
    Writer out=resp.getWriter();
    out.write(n);
}
}

我得到的错误:

未捕获的 SyntaxError:意外的标识符

{数据:来自 Servlet 的 Hello World}

我不知道哪里出错了。请帮我解决这个问题。感谢任何帮助。谢谢

4

2 回答 2

2
String n= '{"data":"Hello World from Servlet"}';
于 2013-08-21T07:54:45.183 回答
0
callback: function (result) { // you are stating you recieve result
        console.log(result);
        if (response.success === true) { // but you are trying to use response, change both to result or response
            Ext.Msg.alert('Link Shortened', response.result, Ext.emptyFn);
        } else {
            Ext.Msg.alert('Error', response.result, Ext.emptyFn);
        }
    }

您返回的 json 似乎也是错误的,请尝试:

callback += "({\"success\":true, \"msj\":" + "\"" + "Exitoooo!" + "\" });"; // where callback is the parameter sent via url  
于 2014-02-19T23:30:19.890 回答