1

I am getting errors on callBacks. I have tried following code in jsfiddle.com . You can also try. Data from servelet is not returning. It's returning same error again and again. Check jquery library when you try in jsfiddle

 $.ajax({
            url : 'http://192.168.16.111:8081/MiddleWareUsman/androidServlet',
            type : "post",

         dataType: "jsonp",
            data : {
        "fname": "chaaaaapiio",
            "lname": "gya"
            },
            success : function(data) {
                alert("hello"+data);

            },
            error : function(xhr, ajaxOptions, thrownError) {

        alert(thrownError);                     
            }
        });

My server side:

                    String a=request.getParameter("fname");
        String b=request.getParameter("lname");

        response.getWriter().write(a+" "+ b);
user3118495
  • 11
  • 1
  • 6

2 Answers2

2

It appears you have a couple problems.

  1. JSONP requests can't be sent via POST. They are actually sent as <script> tag requests anyway which are GET requests.

  2. Your server isn't doing JSONP. For the server to do JSONP, it must wrap the requested data in a call to a javascript function who's name was passed as an argument to the request and then the actual data is passed as an argument to that function. JSONP is a big hack, but it works by requesting a javascript and that's what the server must return.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    where should i change code bro. I am not getting it..Should i change my server side code? – user3118495 Mar 17 '14 at 08:00
  • @user3118495 - Change your ajax call to a GET and then change your server to output JSONP. You will have to do some research on the easiest way to do that in your server environment. Here's one [helpful description of JSONP](http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about). – jfriend00 Mar 17 '14 at 08:01
  • 1
    @user3118495 - in case you didn't realize, your server is NOT reachable from where we are out on the internet so the jsFiddle doesn't help us much. – jfriend00 Mar 17 '14 at 08:03
  • @jfreind request parameter are reaching server. But response is not shown back. Instead it is showing same error – user3118495 Mar 17 '14 at 08:04
  • @user3118495 - that's because your server is NOT returning JSONP. Not much else I can tell you. If you request JSONP, the server must respond with JSONP. A JSONP formatted response is VERY different from a JSON response. JSONP is an actual script (a function call to a particular function name that was passed in the arguments to the server request) that the browser will then execute. – jfriend00 Mar 17 '14 at 08:06
  • @user3118495 - we're not going to write your server code for you. Do some of your own research on how to make your server environment produce JSONP. Once you're doing that, you can come back if you have a specific problem with the JSONP. If your server is `java` based (just guessing based on the tag), then you might want to read [this](http://stackoverflow.com/questions/4683114/sending-jsonp-vs-json-data). – jfriend00 Mar 17 '14 at 08:08
0

you simply can't send a POST request using JSONP

check this link out to see how JSONP works..

ponciste
  • 2,231
  • 14
  • 16