JSON ajax post data - get 400 bad request message - javascript

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}
} )

Related

I get a problem with ajax get request can someone help me?

My code do not response any data to me..
bellow some code:
<script type="text/javascript">
function matriculaFn(mat) {
$.ajax({
method: 'GET',
url:'My url API,
async: true,
crossDomain: false,
contentType: 'application/json;charset=utf-8',
dataType: 'json',
headers: {
"Authorization": "Token xxxprivatexxxxx"
},
options: {
useSSL: true
},
success: function(data) {
console.log(data);
var dados = JSON.stringify(data);
console.log(dados);
$("#IDDOCAMPO").val(data.AlgumaCoisa);
},
//error : function(xhr, status, error) {
// do stuff here
//var data = jQuery.getJSON(xhr.responseText);
//console.log(data);
//}
});
};
</script>
here is the error:
jquery-3.3.1.min.js:2 OPTIONS https://url net::ERR_CERT_DATE_INVALID
Can't comment yet because I don't have enough rep but it looks as though this error is Chrome specific. Have you tried other browsers? If so, try some of the methods listed here: link
Other things you could try are:
crossDomain: true,
or adding:
xhrFields: {
withCredentials: true
},
or because it's a GET request try:
dataType: 'jsonp',

How to get the Excel-File id using the filename in microsoft-graph api?

I am trying to get the File Id using the filename in graph API
I have tried the following thing but this is not working
https://graph.microsoft.com/v1.0/sites/ccc.sharepoint.com,dddddd,eeeeeeeeeeeeeeee/drive/root:/excelDir/filename.xlsx:/children
Got the answer
https://graph.microsoft.com/v1.0/sites/"+webAndSiteId+"/drives/"+folderID+"/root/search(q='filename.xlsx')
Below is the working javascript
var fileCollectionEndpoint = "https://graph.microsoft.com/v1.0/sites/"+webAndSiteId+"/drives/"+folderID+"/root/search(q='"+fileNamesArray[index]+"')"
$.ajax({
url: fileCollectionEndpoint,
async: false,
dataType: 'json',
type: "GET",
cache:false,
headers: {
'Authorization':'Bearer '+token,
},
success: function (json) {
console.log(json.value[0].id);
},
error:function(xhr)
{
}
});

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);
})

Ajax POST request issue with Flask Restless

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);
}
});

Javascript with SharePoint 2013 REST Endpoint "INSERT" into List?

Here are what i'm going to use:
SharePoint 2013
Javascript
REST Endpoint
SharePoint List (called: "Announcements")
WebSite (called: "example.com")
Refs:
http://www.plusconsulting.com/blog/2013/05/crud-on-list-items-using-rest-services-jquery/
https://msdn.microsoft.com/EN-US/library/dn292552.aspx
Very simply:
How do i INSERT a new item (row) inside the List please?
I tried:
$.ajax({
url: "https://example.com/_api/web/lists/getbytitle('Announcements')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify( { '__metadata': { 'type': 'SP.Data.AnnouncementListItem' }, "Title": "New Announcement!" } ),
headers: {
"Accept": "application/json;odata=verbose",
"Authorization": "Bearer " + accessToken
"X-RequestDigest": form digest value,
"IF-MATCH": etag,
},
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
Then i know a lot of things go wrong especially in headers section. But what does it mean by:
Authorization
accessToken
X-RequestDigest
IF-MATCH
.. and then HOW TO get these values (with JavaScript)? So that:
What are always the exact required fields there?
And how/where to get these values from?
I still can not find a simple and complete example about doing this Update / Insert properly.
So there are two ways that I have used to submit an item to a list, the jQuery library SPServices and REST API's. SPServices is really well documented here. Using REST API's is much faster and pretty easy too!
function createListItem(itemProperties, success, failure) {
$.ajax({
url: "https://example.com/_vti_bin/listdata.svc/Announcements",
type: "POST",
processData: false,
contentType: "application/json;odata=verbose",
data: JSON.stringify(itemProperties),
headers: {
"Accept": "application/json;odata=verbose"
},
success: function(data) {
success(data.d);
},
error: function(data) {
// failure(data.responseJSON.error);
alert("error");
}
});
}
First thing I am doing above is creating a function that you can call whenever you want to create a new list item. The parameter itemProperties can be populated with the fields which you need, see below.
var Title = "Title";
var Answer = "Answer";
var userid = _spPageContextInfo.userId;
var taskProperties = {
'Title': Title,
'Answer': Answer,
'UserId': userid
};
Then all we have to do is call this function with the new variable we just declared.
createListItem(taskProperties, function(task) {
alert("Thank you for your input!");
},
function(error) {
console.log(JSON.stringify(error));
}
);
Actually jsfiddle which you have posted in the previous commend is not the REST . you just use the SharePoint client object model. find below the REST API model I hope it will work
var cat = {
"__metadata": { "type": ItemType },
"Title": "GenIT-Issue",
}
$.ajax({
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('Tickets')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(cat),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
},
error: function (data) {
}
});
I run this code inside my SharePoint page so there is no authentication required. it will run on current user privilege

Categories