Ajax POST request issue with Flask Restless - javascript

I'm trying to use jQuery and Ajax to send a POST request to my API.
$.ajax({
url: '/api/Orders',
headers: {
contentType: "application/json"
},
type: "POST",
data: JSON.stringify({'description':'test'}),
});
When I'm using postman (chrome extension) the POST request is fine and everything works great.
If I'm trying to use the above code with AJAX the response is:
message:"Request must have "Content-Type: application/json" header"
I find it really weird because I set contentType : "application/json".

Did you have tried to set the property dataType to JSON?
Your code will looks like:
$.ajax({
url: '/api/Orders',
headers: {
contentType: "application/json"
},
type: "POST",
data: JSON.stringify({'description':'test'}),
dataType: "json"
});

After 6 hours i found the correct answer.
$.ajax({
url: 'http://127.0.0.1:5000/api/Orders',
headers: {
accepts: 'application/vnd.api+json'
},
contentType: "application/json",
type : 'post',
data: JSON.stringify({
'description' : 'test'
}),
dataType: "json",
success: function(data) {
console.log(data);
}
});

Related

How to rewrite ajax get request to post request (jquery)?

I have a GET request:
$.ajax({
url: '/items?ids=' + value.join(','),
method: 'get',
dataType: 'json'
})
How can I create a POST request from this?
$.ajax({
url: 'url_here',
method: 'post',
data: {
fName1: value1,
fName2: value2,
...
},
dataType: 'json'
});
The way of post requests works are different from the get request, in http post request you need to pass the values in the body of your request as bellow :
$.ajax({
url: "/items",
contentType: "application/json",
type: "POST",
data: {
"id": $("#id").val(),
"value1": $("#value1").val(),
"value2": $("#value2").val()
}
});
you can use fiddler to debug and watch what going on the wire.

How to access json object using javascript?

I am not an expert in json parsing so please bear with me if i ask simple question.I have a json response like this :
{
"resp": [{
"Key": "123423544235343211421412",
"id": "12"
}]
}
I want to access value of key and id (123423544235343211421412,12)
.I tried following but i can't get the values!
I appreciate if you guys show me how to get those values.Thanks
var postData = {
Name: "Galaxy",
action: "token"
};
$.ajax("https://someapiurl/getit.aspx",{
type : 'POST',
data: JSON.stringify(postData),
contentType: "application/json",
success: function(data) {
var json = JSON.parse(data);
alert(json.resp[0].Key);
alert(json.resp[1].id);
},
contentType: "application/json",
dataType: 'json'
});
You're almost there. jQuery automatically parses the response as JSON for you when you specify dataType: 'json'
$.ajax('https://someapiurl/getit.aspx', {
method: 'POST',
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
data: JSON.stringify(postData)
}).done(function(obj) {
alert(obj.resp[0].Key);
alert(obj.resp[0].id);
})

JSON ajax post data - get 400 bad request message

First of all. I read many questions about this problem here in stackoverflow but nothing helps me.
I've this function:
function ip_login(){
//alert(JSON.stringify(arr_login));
$.ajax({
crossdomain: true,
url: 'https://****/Token',
type: 'POST',
data: JSON.stringify(arr_login),
contentType: 'application/x-www-form-urlencoded',
dataType: 'json',
success: function (data) {
console.log(data);
}
});
}
And this is my JSON string:
{"grant_type":"password","username":"mail#mail.de","password":"blabla"}
With the google chrome extension 'postman' it works fine!
If I test my code above I got the popular 400 bad request message.
When I take the code of the extension 'postman' it doesn't work too.
This is the code of postman:
var settings = {
"async": true,
"crossDomain": true,
"url": "https://*****/Token",
"method": "POST",
"headers": {
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"grant_type": "password",
"username": "mail#mail.de",
"password": "blabla"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
I can't fine my issue..
I searched on Google and SO but no reply helped me.
Edit:
This is in my console:
"NetworkError: 400 Bad Request - https://awesome-url.de/Token"
The problem is in stringifiing JSON. Server expects json but you send string(stringified JSON). Try
function ip_login(){
//alert(JSON.stringify(arr_login));
$.ajax({
crossdomain: true,
url: 'https://****/Token',
type: 'POST',
data: arr_login,
contentType: 'application/x-www-form-urlencoded',
dataType: 'json',
success: function (data) {
console.log(data);
}
});
}
Not sure if this helps, but try anyways.
When I'm sending Json, I didn't use "JSON.stringify(arr_login)"
All I did was something like "data:{arr_login:arr_login}"
This is how my variable looks like.
var req = new Object();
req["type"] = 'refresh';
req["time"] = window.page_ordertime;
This is how my ajax looks like
$.ajax( {
url:"listorder_process.php",
method: "POST",
dataType:"json",
data:{request:req}
} )

ERROR: bad URI when sending data as JSON with GET method to a Rails resource

Hello I'm performing a GET request on a RESTful Rails resource, like so:
function getGroups(category) {
$.ajax({
type: 'GET',
url: 'http://localhost:3000/groups.json',
data: JSON.stringify({"access_token":"569669d8df0456", "category":category }),
success: function(data) {alert(data)},
contentType: "application/json",
dataType: 'json'
});
});
getGroups("own_groups");
The problem is that Webrick server errors out like this:
ERROR bad URI
`/groups.json?{%22access_token%22:%22569669d8df0456%22,%22category%22:%22own_groups%22}'.
It must be something related with how the JSON data is parsed, because I am having no problems with another GET request WITHOUT JSON data, and a POST request WITH JSON data...
Update: adding code for POST request (where JSON.stringify is required)
function addGroup(name, description) {
$.ajax({
type: 'POST',
url: 'http://localhost:3000/groups.json',
data: JSON.stringify({"access_token":"569669d8df0456", "group_name":name, "group_description":description}),
success: function(data) { alert("ok")},
contentType: "application/json",
dataType: 'json'
});
};
addGroup("nice group", "full of people");
Do not use JSON.stringify. Simply put:
data: {"access_token":"569669d8df0456", "category":category },
Moreover, you do not need to specify complete url http://localhost:3000/groups.json, it can be just simply groups.json

javascript jquery ajax handle response with jsp format by using xml post method

I would like to ask whether can get specific value inside the response data in JSP format by ajax.
Below is my source code:
$.ajax({
type: 'POST',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
url: rootURL,
dataType: "xml",
data: postParameter(),
complete:function(data){
if (data.readyState == 4 && data.status == 200) {
var response = data.responseText;
}
},
});
Please refer below for the response data from server
<?xml version="1.0" encoding="UTF-8" ?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body><MYTesting_AppResponse xmlns="WebServices">
<return>
<status>success</status>
<resourceMessageBean>
<rsa_note3>To proceed, please enter your password and click "Login"</rsa_note3>
<phrase>???en.common.password.login.p3???</phrase>
<username>Username</username>
<password>Password</password>
</resourceMessageBean>
<loginFormBean>
<username>tester1</username>
</loginFormBean>
</return>
</MYTesting_AppResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
May i know how to use js to get "resourceMessageBean" from the response?
Another response from server:
<SOAP-ENV:Body>
<M2UPayLogin_AppResponse xmlns="WebServices">
<return>
<status>success</status>
<resourceMessageBean>
<rsa_note3>To proceed, please enter your password and click "Login"</rsa_note3>
<phrase>Phrase</phrase>
<username>Username</username>
<password>Password</password>
</resourceMessageBean>
<loginFormBean>
<username>tester1</username>
</loginFormBean>
<navigationsBean>
<login>
Login
</login>
</navigationsBean>
</return>
</M2UPayLogin_AppResponse>
How to get the "www.google.com" from navigationsBean, because i use the method below only showing the text "Login" only.
$.ajax({
type: 'POST',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
url: rootURL,
dataType: "xml",
data: postParameter(),
complete:function(xml){
$(xml).find('resourceMessageBean').each(function(){
var username = $(this).find('username').text();
});
}
});
Update:
$.ajax({
type: 'POST',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
url: rootURL,
dataType: "xml",
data: postParameter(),
complete:function(xml){
$(xml).find('navigationsBean').each(function(){
var username = $(this).find('login a').text();
});
}
});

Categories