1

Here is what I am trying :

On client side :

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);
        }
    }
});

On other domain(Server-side) :

public class HelloWorldServlet extends 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);
}
}

Error I am getting :

Uncaught SyntaxError: Unexpected identifier

{data:Hello World from Servlet}

I am not getting where I am going wrong.Please help me resolve this issue.Any help is appreciated.Thanks

Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
Dev
  • 3,922
  • 3
  • 24
  • 44
  • possible duplicate of [parsererror after jQuery.ajax request with jsonp content type](http://stackoverflow.com/questions/5359224/parsererror-after-jquery-ajax-request-with-jsonp-content-type) – Evan Trimboli Aug 21 '13 at 07:45

2 Answers2

2
String n= '{"data":"Hello World from Servlet"}';
Vlad
  • 3,626
  • 16
  • 14
  • Edited to : "{'data': 'Hello World from Servlet}"; But getting error "Unexpected token :" – Dev Aug 21 '13 at 08:02
  • @Daemon A value can be a string in **double quotes**, or a number, or true or false or null, or an object or an array: http://www.json.org/ – Vlad Aug 21 '13 at 08:05
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);
        }
    }

Also the json you return seems to be wrong, try:

callback += "({\"success\":true, \"msj\":" + "\"" + "Exitoooo!" + "\" });"; // where callback is the parameter sent via url  
code4jhon
  • 5,725
  • 9
  • 40
  • 60