0

I am sending the below ajax request to server which is running in local host. Before sending request i tested the request using REST CLIENT in Firefox iam getting the response and the status also 200. But when i make the request from jquery iam getting error. I tired to figure out the error using firebug iam getting bad request. Am i missing anything?

         $.ajax({

             type: "POST",
             url: "http://10.8.2.18:8080/myservice/services/listObject",
             data: {    "userAgent":null,
                        "name":"xxx",
                        "delimiter":"/",
                        "marker":null,
                        "prefix":"localmedia/my_datas/"
                   },
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: function (data) {
                 alert('Success');
                 var json = $.parseJSON(data);
                 alert(json);
             },
             error: function (data, status, error) {
                 console.log(data);
                 console.log(status);
                 console.log(error);
                 alert("error");
             }
         });
     });
shanthi_karthika
  • 915
  • 2
  • 8
  • 22
  • your server must support cross domain requests – Cris Jan 07 '14 at 07:32
  • same origin policy? see this. http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy – soredive Jan 07 '14 at 07:33
  • Can you post the error details? – kvc Jan 07 '14 at 07:42
  • iam using jquery from google cdn.. Will it creates the error...? – shanthi_karthika Jan 07 '14 at 08:45
  • "NetworkError: 400 Bad Request - http://10.8.27.18:8080/myservice/services/listObject?callback=jQuery1102020351383240086462_1389171576132&{%22userAgent%22:null,%22name%22:%22XXX%22,%22delimiter%22:%22/%22,%22marker%22:null,%22prefix%22:%22localmedia/my_datas/%22}&_=1389171576133" error iam getting in firebug – shanthi_karthika Jan 08 '14 at 09:03

2 Answers2

0

Try:

var request={    "userAgent":null,
                        "name":"xxx",
                        "delimiter":"/",
                        "marker":null,
                        "prefix":"localmedia/my_datas/"
                   };

$.ajax({

             type: "POST",
             url: "http://10.8.2.18:8080/myservice/services/listObject",
             data: JSON.stringify(request),
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: function (data) {
                 alert('Success');
                 var json = $.parseJSON(data);
                 alert(json);
             },
             error: function (data, status, error) {
                 console.log(data);
                 console.log(status);
                 console.log(error);
                 alert("error");
             }
         });
     });
Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
0

You are passing jQuery an object to convert to URL Form Encoded data:

data: {    "userAgent":null,
           "name":"xxx",
           "delimiter":"/",
           "marker":null,
           "prefix":"localmedia/my_datas/"
      },

But you are saying that you are sending JSON:

contentType: "application/json; charset=utf-8",

If you want to send form encoded data, then remove the contentType override.

If you want to send JSON, then encode your object as such with JSON.stringify:

data: JSON.stringify({    "userAgent":null,
           "name":"xxx",
           "delimiter":"/",
           "marker":null,
           "prefix":"localmedia/my_datas/"
      }),
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335