-1

I have a rest URL where I need to post some JSON data and it would give a response as below screenshot

enter image description here

Now I need to consume this in my application using $http or $.ajax.I used $.ajax as below

var postdata = {} //whole jsonobject which is der in above screenshot      
$.ajax({
    url: "http://10.11.13.153:8081/fro?assettype=00000000-0000-0000-0000-000000031026",
    type: 'post',
    data: postdata,
    dataType: 'json',
    success: function (data) {  
        console.log("response ",JSON.stringify(data));
    }
});

But for this I get 200 response but I don't see that response consoled in success function.

In the network tab I see the below response

terms%5Bitems%5D%5B0%5D%5B_type%5D=term&terms%5Bitems%5D%5B0%5D%5B_id%5D=6662c0f2.e1b1ec6c.1mdln1e8h.0ibdeqc.s1nm3u.vbt90ta1civ5asp5qetu5&terms%5Bitems%5D%5B0%5D%5B_url%5D=https%3A%2F%2F10.11.13.155%3A9445%2Fibm%2Fiis%2Figc%2F%23dossierView%2F6662c0f2.e1b1ec6c.1mdln1e8h.0ibdeqc.s1nm3u.vbt90ta1civ5asp5qetu5&terms%5Bitems%5D%5B0%5D%5B_name%5D=Export_02

which is not in JSON format and not is not expected.

Below is the screenshot of headers in N/w tab

enter image description here

In my request headers why do I see form-urlencoded type?

I understood that it takes by default. Then how to change and make it take application/json?

Also as a try instead of JSON as datatype I changed as

datatype:'html'

Then this time I'm getting the same response as above, but this time it is getting consoled in success function. Why so? What can be done here to get the expected response in JSON format. Or anything headers that needs to be added at server side. Help or ideas please

user7350714
  • 365
  • 1
  • 6
  • 20
  • can you try to use data : Json.stringfy(postdata) – hasan Jul 11 '17 at 12:14
  • `success: function (data) { console.log("response ",JSON.stringify(responsedata)); }` your success function returning your data as `data` but you trying to console.log `responsedata` – Zaphiel Jul 11 '17 at 12:15
  • @Zaphiel sry I didnt noticed just typo...Even data is not getting consoled – user7350714 Jul 11 '17 at 12:18
  • *"`var postdata = {} //whole jsonobject which is der in above screenshot`"* That isn't a "JSON object." That's an object. JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Jul 11 '17 at 12:20

1 Answers1

1

Perversely, dataType does not control the type of data; it tells jQuery what type of data you expect to get back. contentType is what tells jQuery what kind of data you're sending. (See the documentation.) And when you set contentType, it's down to you to ensure that what you give jQuery is indeed in that format.

So in your case, you need to set contentType and stringify the object (see *** lines):

var postdata = {} //whole jsonobject which is der in above screenshot      
$.ajax({
    url: "http://10.11.13.153:8081/fro?assettype=00000000-0000-0000-0000-000000031026",
    type: 'post',
    data: JSON.stringify(postdata), // ***
    contentType: 'json',            // ***
    success: function (data) {  
        console.log("response ",JSON.stringify(data));
    }
});

(I also changed the content of success in light of your comment that using responsedata there was just a typo unrelated to the question.)

If you're also expecting JSON back, ensure that the response has the right Content-Type header. (Or, as a second-class option, specify dataType: "json" to tell jQuery it's JSON regardless of what the server says.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875