When executing the following code:
url= "http://192.168.2.171/LoginAuthentication";
$.ajax({
url: 'url',
type: 'GET',
success: function(res) {
var headline = $(res.responseText).find('a.tsh').text();
alert(headline);
}
});
I get this error:
Object XMLHttpRequest cannot load 'url'. Origin null is not allowed by Access-Control-Allow-Origin.
Browsers do not allow you to request resources from another domain (images and script files are the notable exceptions to this rule). Refer to this documentation for details and workarounds.
url="http://192.168.2.171/LoginAuthentication";
$.ajax({
url: url,
type: 'GET',
success: function(res) {
var headline = $(res.responseText).find('a.tsh').text();
alert(headline);
}
});
But it may not work if you are not making a request to the same domain
Related
I am currently trying to obtain this JSON array from
http://d.yimg.com/aq/autoc?query=goo®ion=US&lang=en-US
However, which ever I use, it does not seem to work.
Using
$.ajax({
type: 'GET',
url: 'http://d.yimg.com/aq/autoc?query=goo®ion=US&lang=en-US',
cache : false,
dataType : 'json',
success: function(data){
alert('got it!');
}
}
And also tried
$.getJSON("http://d.yimg.com/aq/autoc?query=goo®ion=US&lang=en-US, function(data) {
});
As well as looking into a bypass for cross domain, and I tried using a proxy, not did not work, Maybe I am implementing it wrong? I can not seem to properly obtain the data (nothing return to data).
EDITED
I used this bypass php proxy to try and bypass the missing CORS and JSONP, and it does not seem to return the data correctly either (I followed an example). The php is within my directory.
$(document).ready(function() {
$("#tickBox").on("input", function(e) {
'use strict';
var tick = document.getElementById("tickBox").value;
$.ajax({
type: 'GET',
url: 'http://d.yimg.com/aq/autoc?query=goo®ion=US&lang=en-US',
crossOrigin : true,
//dataType : 'json',
context: {},
success: function(data){
alert(data.ResultSet.Result[0].name);
}
});
});
});
I am stumped as to how to solve/diagnose ajax/jquery error.
This is my function:
var LogIn = {
Email: $("#Name").val(),
MobileNo: $("#txtMobileNumber").val(),
PinCode: '',
Message: '',
Success:false
};
$.ajax({
type: "POST",
crossDomain: true,
dataType: 'jsonp',
contentType: "application/json",
url: "https://a different server domain/api/LoginRequest",
data: JSON.stringify(LogIn),
success: function (data) {
$("#divError").html(data);
},
error: function (error) {
jsonValue = jQuery.parseJSON(error.responseText);
$("#divError").html(jsonValue);
}
});
I get this error:
jQuery doesn't support using POST and jsonp and the reason for that is very simple: when you inject a <script> tag into the DOM (which is what jQuery does when you use jsonp), the browser will send a GET request to the remote endpoint which has been referred to in the src property of this tag.
So basically you will need to use GET instead:
type: "GET"
Also since the data is sent as query string parameters you should remove the content type:
contentType: "application/json",
and do not JSON.stringify the data.
And here's the full example:
$.ajax({
type: "GET",
crossDomain: true,
dataType: 'jsonp',
url: "https://a different server domain/api/LoginRequest",
data: LogIn,
success: function (data) {
$("#divError").html(data);
},
error: function (error) {
jsonValue = jQuery.parseJSON(error.responseText);
$("#divError").html(jsonValue);
}
});
Of course this will work only if the remote endpoint supports JSONP and the GET verb.
Personally I would recommend using CORS instead of JSONP as it would give you much more options. You will be able to use POST in this case. Please refer to the following material as it seems you are using ASP.NET Web API on the server and trying to make a cross domain AJAX call: http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api
I am trying to access and API using jquery/post but its not working in IE8. Its throwing Access denied error in IE8 only.
js code:
var url = 'http://somecomp.cartodb.com:80/api/v1/map?map_key=xxxxxxxxxxxxxxxxxxxx&stat_tag=API';
var data = //some long data of length greater than 3000
$.ajax({
crossOrigin: !0,
type: "POST",
method: "POST",
dataType: "json",
contentType: "application/json",
url: url,
data: JSON.stringify(data),
success: function(a) {
console.log('success');
},
error: function(a) {
console.log('error');
}
})
If I add ?callback=? at the end of url, it still fires the error callback but with statusText: 'success' and code: 200
here is full code: http://textuploader.com/ato0w
Change dataType to jsonp will allow you to make cross-domiain requests. This will work only with GET requests.
If you're using CORS for accessing cross-origin resource, try to add the following line:
$.ajax({
crossDomain: true, // replace "crossOrigin: !0;"
});
If this not working for you, try to add the following line above $.ajax() call.
jQuery.support.cors = true;
I am trying to make an AJAX call to an API which requires an HTTP header (REST parameters).Currently no data is being returned. I think what is giving the most difficulty is understanding setRequestHeader, not even sure if its necessary. In this example , msdn it takes 2 string arguments: oReq.setRequestHeader("Content-Type", "text/xml") but then where does the authorization header go? Please Help
Currently I have this:
var baseURL = "https://api.azuga.com/azuga-ws/v1/live/location.json";
var header = "Authorization: Basic 0JRGDJW587832"; //Made up number
$.ajax({
url: baseURL,
dataType: 'json',
beforeSend: function(xhr){xhr.setRequestHeader(header);},
success: function(data){
console.log(data);
}
});
Did you tried:
var baseURL = "https://api.azuga.com/azuga-ws/v1/live/location.json";
$.ajax({
url: baseURL,
dataType: 'json',
headers: { 'Authorization': 'Basic 0JRGDJW587832' },
beforeSend: function(xhr){xhr.setRequestHeader(header);},
success: function(data){
console.log(data);
}
});
?
also note that your header is a string, so setRequestHeader is not taking two parameters!
I have the following code:
$("form").submit(function()
{
//Checking data here:
$("input").each(function(i, obj)
{
});
alert(JSON.stringify($(this).serializeArray()));
var url='http://127.0.0.1:1337/receive';
$.ajax({
url: url,
type: 'POST',
contentType:'application/json',
data: JSON.stringify($(this).serializeArray()),
dataType:'json'
});
});
And after I submit the form, I get a JavaScript alert with the json string, so that is made correct (on my server I only log it so it does not matter what it is in it). If I try to send a request to the same link from postman it works, it logs it.
I think I'm doing something wrong in the ajax call, but I am not able to figure out.
Try below piece of code. Add success and error handler for more details
$("form").submit(function()
{
//Checking data here:
$("input").each(function(i, obj)
{
});
alert(JSON.stringify($(this).serializeArray()));
var url='http://127.0.0.1:1337/receive';
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify($(this).serializeArray()),
dataType:'json',
success : function(response) {
alert("success");
},
error: function (xhr, status, error) {
alert(error);
}
});
});
data:{ list : JSON.stringify($(this).serializeArray())}
From the Jquery docs:
Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.
crossDomain attribute simply force the request to be cross-domain. dataType is jsonp and there is a parameter added to the url.
$.ajax({
url:'http://127.0.0.1:1337/receive',
data:{ apikey: 'secret-key-or-any-other-parameter-in-json-format' },
dataType:'jsonp',
crossDomain: 'true',
success:function (data) {alert(data.first_name);}
});