简体   繁体   中英

How to retrieve data from a RESTful service as a post request in angular js?

I am new to angular js so please bear with me.

I have a backend rest implementation something like this:

@RequestMapping(value = "/myurl/url", method = RequestMethod.POST)
MessageQueueResponse getInboundQueueMessages (@RequestParam(value = "request", required = true) MessageQueueRequest request)
{
        MessageQueueResponse response = messageQueueServiceImpl.searchInBoundMessageQueue(request);
        return response;
}

I am using this code to call this service from the front end but I am getting for that:

var mydata = [
    {
        "A" : "data1",
        "B" : "data2",
        "C" : "data3"
    }
];

var jsonData ;
    $http({
        method: 'POST',
        url: 'myurl/url',
        data: JSON.stringify(mydata),
        headers: {'Accept': 'application/json', 'Content-Type': 'application/json'}
    }).success(function(data){
        this.data.store = data;
    }).error(function(){
        alert("error");
    });

I am not able to figure out where I am going wrong.

I don't have enough reputation to add comment so adding as answer. sorry.

The blog shows how you can post JSON data to Spring MVC backend. Blog Link . You can try removing JSON.stringify and directly sending object.

$http has a post method that make it very convenient to send data to a server.

$http.post('myurl/url', mydata)
    .success(function(data){
        this.data.store = data;
    }).error(function(){
        alert("error");
    });

You can read more about it at AngularJS: API: $http

There were some mistakes :

  • The json object above is of array type. Which shouldn't be the case.

Also, data need not be strngyfy-ed. It will be automatically converted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM