last day (14 days later) » 

00:51
0
A: how to make dynamic data objects in ajax requests

Mahesh Singh ChouhanHave you tried like this? var content_id = $(this).val(); var data = JSON.stringify({"playlist_content" : {"content_id" : content_id}}); var settings = { "async": true, "crossDomain": true, "url": "https://domain/api/v2/playlists/", "method": "POST", "headers": { "content-type": "a...

i will try now...
@charlietfl thanks for your advice, let me update answer to stringify object to json string
if i do it right. i get a success response... if i do it wrong then i get mixed domain errors
@Omar updated my answer just now, try now
stringify leads to this error 422 (Unprocessable Entity)
i will make a jsfiddle... dont leave yet :)
00:51
@Omar yup that will be easy to fix issue. Thanks let me know the url
jsfiddle.net/c7n34e3x ok var data works but when i try to make it dynamic like var data1... it does not work
hi
hey guys
why dont you just make this:
this is awesome... thanks for helping
make this?
00:54
var data2 = {
playlist_content:{
content_id : content_id
}
};
sorry copy paste wasnt working
make it a plain javascript object
try passing that
that was the first option of my answer
let me try in the fiddle
in the fiddle I get a cors error
400 (Bad Request)
BTW, when i am using dynamic data i am getting this response in ajax

{"message":"Added “Earn What You're Really Worthˮ to playlist playlist!","playlist_content":{"id":10216,"order":21,"created_at":"2017-04-14T00:55:45.635Z","updated_at":"2017-04-14T00:55:45.635Z","playlist_id":"b32dd20b-f5c4-4850-903f-ac813a3013d8","content_id":"ef0a30ef-3198-4ce6-8927-e390257776ed"},"reorder_url":"https://pmp.psft.co/playlists/b32dd20b-f5c4-4850-903f-ac813a3013d8/contents/10216/reorder.json"}
00:56
but when we tried data it worked.... right? but data1 and data2 did not work
thats good share the work
how u do it?
@Mahesh Singh
let me fix this with data1 then i will definitely share with you
thanks
sometimes third party request works fine, sometime it gives 404 with this url

https://pmp.psft.co/api/v2/playlists/ef0a30ef-3198-4ce6-8927-e390257776ed/contents?access_token=2b28ef0a4d7179185ba596361944739307e8d9427cd745948047c3a8e96b26f8&playlist_id=ef0a30ef-3198-4ce6-8927-e390257776ed
01:07
i got it working
$("select#addto_select").on('change',function(){

var playlist_id = $(this).val();
var content_id = $('.item_id').html();
var content_id = "51ce5736-42f9-4cb8-842c-90bc99146c84";
var token = "2b28ef0a4d7179185ba596361944739307e8d9427cd745948047c3a8e96b26f8";
var data = "{\"playlist_content\" :{\"content_id\" : \"ef0a30ef-3198-4ce6-8927-e390257776ed\"}}"

var data2 = {
playlist_content:{
content_id : content_id
}
};

var url = "https://pmp.psft.co/api/v2/playlists/"+playlist_id+"/contents?access_token="+token+"&playlist_id="+playlist_id;
got a response: {message: "Added “Earn What You're Really Worthˮ to playlist My Financial Playlist!",…}
message
:
"Added “Earn What You're Really Worthˮ to playlist My Financial Playlist!"
playlist_content
:
{id: 10221, order: 1, created_at: "2017-04-14T01:08:19.116Z", updated_at: "2017-04-14T01:08:19.116Z",…}
reorder_url
:
"https://pmp.psft.co/playlists/8efb8662-d4ad-4683-bbdf-3bb3e80e93db/contents/10221/reorder.json"
i dont understand. u are using "data" but console.log says "data2"
check here
https://jsfiddle.net/c7n34e3x/3/
it always works
yes!
post your answer i will vote
vote up on my question
thanks!
the problem is with playlist_id and content_id
i am using this

var playlist_id = 'b32dd20b-f5c4-4850-903f-ac813a3013d8';//$(this).val();
var content_id = "ef0a30ef-3198-4ce6-8927-e390257776ed";
otherwise it gives 404 not found
so the request with JSON.stringify is working fine, only we need to use proper credentials to make it work
ok sure let me update my answer, marked it and also upvote it, will do the same :)
Done updated
Hey Omar Did you upvoted?
I upvoted, waiting for your approval and upvote :)

last day (14 days later) »