Iris CouchDB Post Cross Domain - ajax

I am trying out Iris Couch and I ma having problems posting cross domain.
I am posting some simple JSON from within a chrome extension to Iris' couchdb and I get the dreaded 405 (Method Not Allowed)
I have turned CORS on in the CouchDB config, as well as set CORS credentials TRUE
This is my JS code
_save = function (item) {
var rtrn = $.Deferred();
$.ajax({
type: "POST",
url: 'https://xxxxx.iriscouch.com/notes',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(item),
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function (e) {
rtrn.resolve();
console.log(e);
},
success: function (e) {
console.log(e);
}
});
return rtrn.promise();
}
Thank you!

Related

CSRF token not working in a Laravel ajax call

I've checked the gizillion answers to this question and for some reason I can't get this to work.
I get the error:
401 (Unauthorized)
My route is an api route guarded.
My data:
let ajaxMainTemplate = {
'mainTemplate': mainTemplate,
'templateId': templateId,
'_token': accessToken,
}
My ajax call:
$.ajax({
type: 'POST',
url:`../api/aiMainTemplate/${ajaxData.templateId}`,
data: {
_token: ajaxData._token,
ajaxData
},
success: function(response) {
console.log(response)
}
})
I've tried the above to test based on another response. I put the token outside of the ajaxData object. I get the same error. I've also attempted:
$.ajax({
type: 'POST',
url:`../api/aiMainTemplate/${ajaxData.templateId}`,
headers: { 'X-CSRF-TOKEN': ajaxData._token },
data: ajaxData,
success: function(response) {
console.log(response)
}
})
Same.
I've also confirmed the token is there by adding a console.log Any ideas what I'm doing wrong with this?
My url was behind the api protected route so I had to do an ajax call to login into it via ajax.
The credentials are in a variable ajaxData.
$.ajax({
type: 'POST',
url: 'http://127.0.0.1:8000/api/login',
data: ajaxData,
success: function(response) {
shopifyToken = response.success.token;
getListProducts(shopifyToken);
}
});
With the shopify token generated from this login it worked.

400 using Axios to make a post request to Django Rest Framework

I'm working with learning some javascript to make a post to a little rest API I created with django rest. I have a post that works when I was working with jQuery but now I am reimplementing using Vue.js and am hitting a snag. When I do the below post request I get a 400 error and am not sure what I am missing. I would imagine something with my header is incorrect? Any advice would be welcome.
axios({
method: 'post',
url: '/api/matches/',
data: JSON.stringify(data),
headers: {
"X-CSRFToken": csrfToken,
"Content-Type": "application/json"
}
})
jQuery post for reference:
$.ajax({
type: "POST",
url: "/api/matches/",
data: JSON.stringify(final_data),
success: function() {},
error: function(err) {console.log(err)},
contentType: "application/json; charset=utf-8",
dataType: "json",
crossDomain:false,
beforeSend: function(xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
});

Instagram API for posting to relationship not working

I have tried for a day now so I will post - does anyone know why the following code for posting follow relationship using Instagram API wont work?
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: 'https://api.instagram.com/v1/users/<user-id>/relationship?access_token=' + instagramaccesstoken,
data: JSON.stringify({ "action": "unfollow" }),
dataType: "jsonp",
cache: false,
beforeSend: function () {
$("#loading").show();
},
success: function (data) {
alert(data);
$("#loading").hide();
}
});
If you replace the <user-id> placeholder in your URL with a real user ID, then it will work.

Ajax post parameters ASP.NET MVC 3

Hello guys i have the next ajax call for login. I serialize the form and send the data to server and return redirect url link. My problem is that my url after post is like
http://localhost:50802/?username=&password= and not http://localhost:50802/Home
$.ajax({
type: "POST",
url: "Login/Login",
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: loginJson,
cache: true,
async: false,
complete: function (result) {
alert(result.link);
window.location.replace = "/Home/Index";
},
error: function () {
$("#username").val("");
$("#password").val("");
alert("Wrong Username or Password!");
}
}); //end ajax call
It looks like you wrote this $.ajax call in the .click event of a submit button or in the .submit event of a form without canceling the default action by returning false from the callback or by calling preventDefault on the argument. Here's how your code should look like:
$('#id_of_your_form').submit(function(e) {
e.preventdefault(); // <-- That's what I am talking about and what you forgot
$.ajax({
type: "POST",
url: "Login/Login",
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: loginJson,
cache: true,
async: false,
complete: function (result) {
window.location.replace = "/Home/Index";
},
error: function () {
$("#username").val("");
$("#password").val("");
alert("Wrong Username or Password!");
}
}); //end ajax call
});
Also async: false,????? You know what this does, do you? That's not AJAX. That's a blocking synchronous call to your webserver during which the client browser would be frozen like during the Ice Age 2 ruining all user experience.
Try returning false at the end of your submit function
$('#id_of_your_form').submit(function(e) {
$.ajax({
type: "POST",
url: "Login/Login",
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: loginJson,
cache: true,
async: false,
complete: function (result) {
window.location = "/Home/Index";
},
error: function () {
$("#username").val("");
$("#password").val("");
alert("Wrong Username or Password!");
}
}); //end ajax call
return false; });
Another option would of course be to return the correct redirectlink from the controller instead of overriding it in the java script.

No Ajax response even though URL is correct?

i get error in firebug when i try to get the data from the second url, but when i try the url in comments (the first one) everything is ok,
function build()
{
$.ajax({
type: "GET",
contentType: "application/json",
crossDomain: true,
/// url: "http://localhost:9100/todo-0.1/data.json",
url:'localhost:9000/rest/admin/component?componentUri=file:/home/workspace/app/5-Integration/',
dataType: "json",
success: function (data)
{
var n=data.links.link[1]['#href'].indexOf("file");
var ContObject=new Object();
ContObject.data=new Object();
ContObject.data["title"]= decodeURI(data.links.link[1] ['#href'].substring(n));
ContObject.state="open";
ContObject.metadata=new Object();
ContObject.metadata["id"]= decodeURI(data.links.link[1]['#href'].substring(n));
var jsonText = JSON.stringify(ContObject);
var output="<div>";
output+= jsonText;
output+="</div>";
document.getElementById("placeholder").innerHTML=output;
alert("success");
},
error: function (data,status)
{
console.log("ERROR");
console.log(status);
}
});
thanks for guidance
url:'localhost:9000/rest/admin/co....
must be
url:'http://localhost:9000/rest/admin/co....
update:
enable cross domain requests: How to enable cross-domain request on the server?
debug:
success: function (data) { console.log(data); }

Resources