Cross-origin ajax not working - ajax

I am having a problem while using cross-origin ajax.
I know it's a common question but did not get any solution for it yet.
$.ajax({
type: "GET",
url: url,
data: {id:id},
dataType: "jsonp",
crossDomain: true,
contentType: "application/jsonp; charset=utf-8",
async: false,
success: fnsuccesscallbackk,
error: function(xhr, error){
alert(error);
},
jsonpCallback: fnsuccesscallback
});
function fnsuccesscallback(data){
alert(data)
}
…but getting undefined response in callback function.
Is there anything wrong what I am doing.

After a lots of RND finally i got the solution of this.
Ajax function:
$.ajax({
type:"GET",
url:'https://www.url.com/welcome/test_js',
data:{name:'xyz'},
crossDomain:true,
dataType: "jsonp",
jsonp: 'fnsuccesscallback',
success: function(data) {
alert(data.name)
}
});
In the Php function:
function test_js() {
echo $_GET['fnsuccesscallback'] . "(" . json_encode($_GET) . ")";
}

Related

Youtrack - Apply Command to an Issue requires log in

To apply a command to an issue i'm using this code:
$.ajax({
async: false,
type: 'post',
url: "https://golaservices.myjetbrains.com/youtrack/rest/issue/"+e.target.id+"/execute"+"?command="+target.val(),
cache: "true",
dataType: "html"
});
but that brings up this error
You have no access to this resource. Try to log in.
that's why i tried putting that first bit of code like this:
$.ajax({
async: false,
type: 'post',
url: "https://golaservices.myjetbrains.com/youtrack/rest/user/login?login=xxx&password=123",
cache: "true",
dataType: "html",
success: function(output, status, xhr) {
alert(xhr.getResponseHeader('Set-Cookie'));
}
}).done(function (data) {
console.log("in done");
$.ajax({
async: false,
type: 'post',
url: "https://golaservices.myjetbrains.com/youtrack/rest/issue/"+e.target.id+"/execute"+"?command="+target.val(),
cache: "true",
dataType: "html"
});
console.log("after done");
});
but i still get the same error, any idea how to do this ?
/rest/user/login endpoint has been deprecated for a while already and is not recommended for further use. Try permanent tokens instead, they don't require any preflight login requests to be executed.
$.ajax({
type: "post",
url: "https://golaservices.myjetbrains.com/youtrack/rest/issue/" + e.target.id + "/execute" + "?command=" + target.val(),
dataType: "json"
beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Bearer " + permanentToken); },
});

Opposite of success: function(data) - ajax

what is the opposite of -> success: function(data)
$.ajax({
type: "POST",
cache: false,
url: "/p.php",
data: info,
success: function(data){
I want to check if it is not success instead of if it is success...
You catch success situation with success function and there is also error function. Please see the documentation http://api.jquery.com/jquery.ajax/
And please see this for detailed xhr catch mechanizm
$.ajax({
type: "POST",
cache: false,
url: "/p.php",
data: info,
success: function(data){
//do something
},
error: function(jqXHR, textStatus, errorThrown){
console.log(errorThrown);
}

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 JSONP parsererror

I'm trying to use an ajax call to bring back data from a web api. I wrote 2 similar functions and neither work. I can see the data come back through Fiddler, but it won't go to the success call, for both of the functions below. What am I doing wrong? The data comes back in both functions in Fiddler, it just doesn't go to success.
Here is attempt 1:
function PopulateDivisions()
{
$.support.cors=true;
$.ajax({
type:'GET',
url:'http://IP/Service/api/DivisionSearch/GetAllDivisions',
data: {},
contentType: 'application/json; charset=utf-8',
dataType: 'jsonp',
success: function(data) {
alert(data);
$("#divisionSelect").append($('<option></option>').val("-99").html("Select One"));
$.each(data, function(i, item){
$("#divisionSelect").append($('<option></option>').val(item.Name).html(item.Name));
});
},
error: function(xhrequest, ErrorText, thrownError) {
alert("Original: " + thrownError + " : " + ErrorText);
}
});
}
Error: jQuery19102671239298189216_1382022403977 was not called : parser error
Here is the data Fiddler is showing comes back:
[{"Id":1,"Description":"Executive","Name":"Executive "},{"Id":2,"Description":"ASD","Name":"Administrative Services Division "},{"Id":3,"Description":"COM","Name":"Communications "},{"Id":4,"Description":"CP","Name":"Contracts and Procurement "},{"Id":5,"Description":"PMD","Name":"Program Management Division "},{"Id":6,"Description":"RED","Name":"Research and Evaluation Division "},{"Id":7,"Description":"IT","Name":"Information Technology "}]
Here is attempt #2:
function PopulateDivisions2()
{
$.support.cors=true;
$.ajax({
type:'GET',
url:'http://IP/Service/api/DivisionSearch/GetAllDivisionsJsonP',
data: {},
contentType: 'application/json; charset=utf-8',
dataType: 'jsonp',
jsonp: false,
jsonpCallback: "myJsonMethod",
success: function(data) {
//data = JSON.parse(data):
alert(data);
$("#divisionSelect").append($('<option></option>').val("-99").html("Select One"));
$.each(data, function(i, item){
$("#divisionSelect").append($('<option></option>').val(item.Name).html(item.Name));
});
},
error: function(xhrequest, ErrorText, thrownError) {
alert("PopulateDivisions2: " + thrownError + " : " + ErrorText);
}
});
}
Error: myJsonMethod was not called : parsererror
Here is the data Fiddler shows is coming back:
"myJsonMethod([{\"Id\":1,\"Description\":\"Executive\",\"Name\":\"Executive \"},{\"Id\":2,\"Description\":\"ASD\",\"Name\":\"Administrative Services Division \"},{\"Id\":3,\"Description\":\"COM\",\"Name\":\"Communications \"},{\"Id\":4,\"Description\":\"CP\",\"Name\":\"Contracts and Procurement \"},{\"Id\":5,\"Description\":\"PMD\",\"Name\":\"Program Management Division \"},{\"Id\":6,\"Description\":\"RED\",\"Name\":\"Research and Evaluation Division \"},{\"Id\":7,\"Description\":\"IT\",\"Name\":\"Information Technology \"}]);"
contentType: 'application/json; charset=utf-8' Tells your server that you are sending JSON data, but you don't have any data you are sending. Try leaving that setting out.
If you were to brows to your url in the browser do you get json back?
I'm not sure if this would matter, but I would remove the error setting because it says in the jQuery Ajax documentation that This handler is not called for cross-domain script and cross-domain JSONP requests.
I would try to run this with the least amount of configuration like this:
$.ajax({
url:'http://IP/Service/api/DivisionSearch/GetAllDivisions',
dataType: 'jsonp',
success: function(data) { console.log(data); }
});
See if this works and then build on top of it. Without jsfiddle it's hard to debug what's going on.
Here is a link that should be a good resource for you: Basic example of using .ajax() with JSONP?
function PopulateDivisions2(){
$.support.cors=true;
$.ajax({
type:'GET',
url:'http://IP/Service/api/DivisionSearch/GetAllDivisionsJsonP?callback=?',
data: {},
contentType: 'application/json; charset=utf-8',
dataType: 'jsonp',
jsonpCallback: "myJsonMethod" });
function myJsonMethod(data) {
//data = JSON.parse(data):
alert(data);$("#divisionSelect").append($('<option></option>').val("-99").html("Select One"));
$.each(data, function(i, item){
$("#divisionSelect").append($('<option></option>').val(item.Name).html(item.Name));
});
}}
Can you try the above code? I have removed the success callback and included callback in query string.

jQuery ajax POST request with application/json

I want to make a POST request to the remote server from jQuery. When I write code like this
$.ajax({
type: 'POST',
url: 'http://mysite:8080/orderService/order/send',
crossDomain: true,
data: JSON.stringify(orderSendRequest),
dataType: 'json',
success: function(responseData, textStatus, jqXHR) {
var value = responseData.someKey;
},
error: function (responseData, textStatus, errorThrown) {
alert('POST failed.');
}
});
Everything is ok, but I want ContextType to be application/json, and when I add this line to the code the request doesn't work and I have the following error:
XMLHttpRequest cannot load http://mysite:8080/orderService/order/send. Origin null is not allowed by Access-Control-Allow-Origin.
$.ajax({
type: 'POST',
url: 'http://mysite:8080/orderService/order/send',
crossDomain: true,
data: JSON.stringify(orderSendRequest),
dataType: 'json',
contentType : 'application/json; charset=utf-8',
success: function(responseData, textStatus, jqXHR) {
var value = responseData.someKey;
},
error: function (responseData, textStatus, errorThrown) {
alert('POST failed.');
}
});
I don't believe json supports crossDomain. Research using jsonp datatype instead.

Resources