Failed to load resource: net::ERR_FAILE - javascript

my code is this:
$.ajax({
type: 'Get',
url: 'https://kf.kobotoolbox.org/assets/a7GG9SXYtLTCS4FLm4mjjP/submissions/?
format=json',
dataType : 'jsonp',
success: function(data) {
console.log(data);
},
error: function(data) {
console.log("error");
}
});
When I enter the url on web browser I got the data
but I can not access with ajax

Related

how can I authenticate to a rest web service by ajax

I want to send username and password to a Rest web service to recover the Token. I tested this code but always sends me to error function
$.ajax({
type: "POST",
url: "http://192.168.1.89:1111/connect/token",
dataType: 'json',
async: false,
data: '{}',
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', btoa(username, password));
},
success: function() {
alert('Thanks for your comment!');
},
error: function(xhr, status, error) {
alert(error);
}
});

Ajax returns error "Failed to load resource: net::ERR_EMPTY_RESPONSE" when response does not arrive within 1 min

I am facing very strange issue.I have written django project in which I am sending ajax request.If response comes before 1 min then ajax is working fine but if response does not come within 1 min then ajax returns error "Failed to load resource: net::ERR_EMPTY_RESPONSE".
My ajax code is as below:
$.ajax({
url: '/index/',
type: 'POST',
timeout: 60000000,
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(finalJson),
dataType: 'text',
success: function(response){
// Get response from django server
// do your code here
off();
console.log("Got response");
console.log(response);
if(response.includes("documents will be deleted"))
{
console.log("response!!!");
if (confirm(response + '.Continue' + '?'))
{
document.getElementById("overlay").style.display = "block";
document.getElementById("loader").style.display = "block";
console.log("yes");
$.ajax({
url: '/index/',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({"logpaths":jsonobj,"emails":mailobj,"cu_hostname":cu_host,"user_tag":us_tag,"delete_s_date":delete_s_date,"delete_e_date":delete_e_date,"delete_docs":true}),
dataType: 'text',
success: function(response)
{
off();
alert(response);
}
});
}
}
else
{
alert(response);
}
//document.getElementById("response_div").innerHTML = response;
}
});
Please help me.

Ajax, failed to load PHP from server

I'm trying to make a PHP connection, but I keep getting this error. I am hoping someone can help.
My code gives the following error:
{
"readyState": 0,
"status": 0,
"statusText": "NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost/php/busca.php'."
}
My code is:
SendSQL.onclick = function() {
var dataString='a=hello&b=world';
$.ajax({
type: "POST",
url:"http://localhost/php/busca.php",
data: dataString,
async: false,
dataType:'html',
success: function(data){
alert(data);
},
error: function(data){
alert(JSON.stringify(data)); //Better diagnostics
}
});
};
And the file busca.php is:
<?php
$a = $_POST['a'];
$b = $_POST['b'];
echo "$a, $b";
?>
Try this approach...
SendSQL.onclick = function() {
var dataString='a=hello&b=world';
$.ajax({
type: "POST",
url:"http://localhost/php/busca.php",
data: {
"a":"hello",
"b":"world"
},
async: false,
dataType:'text',
success: function(data){
alert(data);
},
error: function(data){
console.log(data);
}
});
};
Your dataString is the way for sending parameter of GET request.
Let's change to JSON like
var dataString = { "a":"hello", "b":"world" };
$.ajax({
type: "POST",
url:"http://localhost/php/busca.php",
data: {data: JSON.stringify(dataString)},
async: false,
dataType:'json',
success: function(data){
alert(data);
},
error: function(data){
alert(JSON.stringify(data)); //Better diagnostics
}
});
And in php code, use json_decode($_POST['data'])

Display page only if ajax call succeeds?

I have a subdomain protected by a service similar to cloudflare. It checks if a visitor is human or not. To save bandwidth usage I want only one file to be loaded from this subdomain as indicator if the visitor is a bot or not. If it is not loaded, the service has blocked it as bot, and nothing should be shown.
I already thought of an ajax request before the page loads to check this.
var success = 0;
function myCall() {
var request = $.ajax({
url: "sub.example.com/hello.php",
type: "GET",
dataType: "html"
});
request.done(function() {
// Load page, because the request was successfull
$.ajax({
url: 'content.php', //This is the current file
type: "POST",
dataType:'json', // add json datatype to get json
data: ({load: true}),
success: function(data){
console.log(data);
}
})
});
Is there a better way to do this?
You may try this:
var success = 0;
function myCall() {
var request = $.ajax({
url: "sub.example.com/hello.php",
type: "GET",
dataType: "html",
success: function(){
$.ajax({
url: 'content.php', //This is the current file
type: "POST",
data: dat,
success: function(data){
console.log(data);
},
error: function(data){
console.log('Error!');
}
});
},
error: function(data){
console.log('Error!');
}
});
}

Send headers in JQuery Ajax Get Request

Here is my code.
$.ajax(this.url, {
type: "GET",
//dataType: "json",
beforeSend: function (xhr) {
xhr.setRequestHeader("x-token", token)
},
success: function (data) {
console.log(data);
},
error: function(){
console.log('error');
}
});
I am not able to send ajax request by that code. I also try hearder: {"x-token": token}, In place of beforeSend: but its also not working for me.

Categories

Resources