How to post data from AngularJS to Struts2 using REST

后端 未结 1 461
长发绾君心
长发绾君心 2021-01-28 13:16

I need to get the value from client side to server side. I am using AngularJS and Struts2 REST. Newbie in AngularJS here. My controller doesn\'t get the value passed or am i wro

相关标签:
1条回答
  • 2021-01-28 13:55

    Since angular service is sending JSON, you can send a JSON object using data property. The JSON object will be deserialized to your model class properties because you are using ModelDriven or action class properties otherwise.

    Struts2 REST plugin is made to handle JSON content from the request. It's possible due ContentTypeInterceptor, which is called rest, be able to find JSON handler for the request. The JSON handler is guessed from the Content-Type header or extension to the file in the URL. You can use either ways, but second is easier.

    Note: that URLs without a file extension the Struts2 REST plugin defaulting to handle xhtml content. So you can modify your code

    $http({
        method: "POST",
        url: "api/additionalcategory.json",
        data: {}
    }).success(function(data, status, header, config){
        //success
    }).error(function(data, status, header, config){
        //error
    });
    

    0 讨论(0)
提交回复
热议问题