0

I'm trying to get application/json data sets from an ajax call. Using the same setting that I initially tested on Postman where I got the data normally from the request, I can't get the proper result that I get from Postman with my JavaScript code.

It's a post request and the Content Type is application/json.

The request body's empty. Not sending any parameter.

Header information I get from the postman request.

Following is the source I'm trying to work out.

$.ajax({
    url: myUrl,
    type: "POST",
    contentType: "application/json",
    success:function(res){
        alert(res);
    },
    error:function(request,status,error){
        alert(request+" : "+status+" : "+error);
    }
});

Execute the source above, it goes into the error part and I get the alert message like ' [object Object] : error : '. [object Object] for request, error for status, and nothing for the error.

I've tried to put different settings there like async, cache, tried to put the contentType in a separated header setting like header: {"Content-Type":"appplication/json"} but none of them has worked yet.

This is the failed request header on the google chrome developer tool.

How can I make it work? Thank you ahead for helps.

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
JennyC
  • 3
  • 3

3 Answers3

0

you are just sending empty Json request. you must have some request data like :

data: '{"orgId":"1111"}'

for more info visit jQuery posting valid json in request body

Manish Mehta
  • 71
  • 3
  • 12
0

use the $.ajaxSetup({}); before the ajax request.

collins
  • 76
  • 5
0

Why you want type as POST, change it to GET. Have you tried with type as GET I have implemented similar ajax call which works fine

$.ajax({
    type : "GET",
    url : myUrl,
    async : false,
    success : function(response) { // we have the response
        alert();
    }
    error:function(e){
        alert();
    }
});
Rmahajan
  • 1,311
  • 1
  • 14
  • 23