2

This is my ajax function

$("#submitButtonNc").click(function(){
    var newCreditLimit=$('#newCreditLimit').val();
    var cardNumber=$('#selectedCard').val();

    $.ajax({
        type : 'POST',
        url : 'updateSupplymentaryCreditLimit.json',
        dataType: "json",
        data : {
            'cardNumber' :cardNumber,
            'newCreditLimit':newCreditLimit
        },

        success : function(json) {
            alert("aassdd");

        },
        error:function(error){
            alert("error",error.toString());

        }
    });

This is my back end java function. I have used spring MVC:

@RequestMapping(value = "updateSupplymentaryCreditLimit", method = { RequestMethod.POST })
public @ResponseBody String  updateSupplymentaryCreditLimit(@RequestParam("cardNumber") String cardNumber,
        @RequestParam("newCreditLimit") String  newCreditLimit){
    String message =null;
    try{
        double creditLimit=Double.parseDouble(newCreditLimit);
        SupplementaryCreditCardLimitBean  supplementaryCreditCardLimitBean =new SupplementaryCreditCardLimitBean();
        supplementaryCreditCardLimitBean.setCardNumber(cardNumber);
        supplementaryCreditCardLimitBean.setNewCreditLimit(creditLimit);
        message= "Success";

        }
        catch(Exception e){
            message= "Error";

        }
    return message;
}

When I click #submitButtonNc button back end updateSupplymentaryCreditLimit function called but response display in front end ajax function

error:function(error){
    alert("error",error.toString());
}

please tell me what is the reason display error alert

VPK
  • 3,010
  • 1
  • 28
  • 35
jhone
  • 85
  • 9
  • Is there any exception thrown while running the code in `updateSupplymentaryCreditLimit` method? – VPK Jan 10 '18 at 06:24
  • Please return the json type from your function, even try to map error also in JSON – SaviNuclear Jan 10 '18 at 06:25
  • No VPK. updateSupplymentaryCreditLimit function return "Success" String – jhone Jan 10 '18 at 06:26
  • So does the `error` function called after ajax complete? If yes, what was printed in `alert("error",error.toString());`? – VPK Jan 10 '18 at 06:28
  • VPK, It was printed this error "parsererror" "SyntaxError: Unexpected token < in JSON at position 0" – jhone Jan 10 '18 at 06:32
  • Possibility of null pointer exception, if `newCreditLimit` is null then `double creditLimit=Double.parseDouble(newCreditLimit);` will throw null pointer exception. Please confirm the exception by adding error in your alert string `message= "Error" + e.getMessage();` – Sandeep Kokate Jan 10 '18 at 06:34
  • I think the problem is with the `dataType : json` in the ajax request. As it might be expecting the json response from the server. As currently it might getting as plain string / HTML. I would recommend to remove the `dataType:json` and try again. – VPK Jan 10 '18 at 06:37
  • Sandeep Kokate ,But data values passed to the back end and updateSupplymentaryCreditLimit function return "Success" String – jhone Jan 10 '18 at 06:40
  • @jhone, could you try again by removing the `dataType:json` property of ajax request? – VPK Jan 10 '18 at 06:43
  • VPK , I have removed dataType:json but it isn't working – jhone Jan 10 '18 at 06:44
  • If you can add the external library, it could give the better solution. Try to add `org.json.JSONObject`. – VPK Jan 10 '18 at 07:00
  • Try these answers solutions from question https://stackoverflow.com/questions/18862066/jquery-ajax-always-returns-error-data-being-added-to-database/19418845 – Henrikki Jan 10 '18 at 08:06
  • How about adding produces = "application/json" in @Requestmapping, – Eldhose Abraham Jan 10 '18 at 09:01

0 Answers0