1

what im trying to do is to send data in JSON using jquery to server and receive it at server side. After that parse it and use the data and again response back to client in json form. Im using Play(web based framework for java) at server side. Here is my code:

Tier.js

var attribs={ 
                id: document.forms["tier"]["id"].value , 
                name: document.forms["tier"]["name"].value ,
                limits: document.forms["tier"]["limits"].value
                };

$.ajax({
        type:"get",
        url: "validateTier.json" ,
        dataType: "json",
        jsonp: 'callback',
        data: attribs,
        contentType: "application/json; charset=utf-8",
        beforeSend: function( xhr ) {
            xhr.overrideMimeType( 'application/json; charset=utf-8' );
          },
        error:function(x,e){
            if(x.status==0){
                alert('You are offline!!\n Please Check Your Network.');
                }else if(x.status==404){
                alert('Requested URL not found.');
                }else if(x.status==500){
                alert('Internel Server Error.');
                }else if(e=='parsererror'){
                alert('Error.\nParsing JSON Request failed.');
                }else if(e=='timeout'){
                alert('Request Time out.');
                }else {
                alert('Unknow Error.\n'+x.responseText);
                }
        },
        success: function(data) {
         alert(data.status);
        },

        }).done(function( msg ) {
              alert( "Data Saved: " + msg );
        }); // End ajax

at Server side:

Map<String,Object> json = new HashMap<String,Object>();
        json.put("status", status);
        json.put("message", msg);
        renderJSON(json);

Output:

You are offline!! Please Check Your Network.

ali raza
  • 13
  • 3
  • use firebug to trace the exact error .. is ajax request is generated or not.. may be there is some error in json dataformat.. follow this..http://stackoverflow.com/questions/1255948 http://www.ibm.com/developerworks/library/wa-ajaxintro10/ – Niranjan Singh Nov 16 '11 at 06:39
  • request is generated and im receiving all the params at server side im processing all the information but when i send it back it showing error. – ali raza Nov 16 '11 at 07:18

2 Answers2

0

It looks to me like you are not actually sending the data to the server as JSON - you appear to just be sending arguments as typical name/value pairs. If your server backend is expecting JSON, that is likely the source of your error.

Jake Feasel
  • 16,785
  • 5
  • 53
  • 66
  • well im sending data to server receiving all params but it is causing problem while receiving at jquery side.server is responsing in following way: {"message":"Value should be a number","status":"2"}. i think it is the problem at jquery side as it is not receiving it. – ali raza Nov 16 '11 at 07:29
  • That's the http response code? Unless I'm mistaken, that not possible since 0 is not a valid response code (eg 200, 301, 302, 404, etc...). Isn't that just some code you defined? – Jake Feasel Nov 16 '11 at 15:54
  • thanks i fixed it. it was actually not sending data to server. – ali raza Nov 21 '11 at 06:55
0

check this json related link on jquery website... may be it will help..to get the error.. may be there are some error on your server side code..

have you checked the error reason from you have implemented this one???

http://www.maheshchari.com/jquery-ajax-error-handling/

0-xhr status ,is it initialized or not that means user is offline. 

check this link to get what have you done wrong..

http://encosia.com/use-jquery-to-catch-and-display-aspnet-ajax-service-errors/

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75