ajax function not fetching url - javascript

I have:
function makeAjaxRequest() {
var url = '/queryjob/dbname/ip';
$.ajax(url,
{
success: alert(response)
});
}
When I do a normal browser request on this url I get a response of 43(just a test response right now)
When I click the button I have to run this function, nothing happens. I don't see a get request in the log at all. Do I have some stupid syntax error or something? I am pretty new to js and ajax. I have another function that works where I get a url and act on the html response code, but this one is killing me so far.

You are not calling $.ajax correctly. Assuming you wanted to make a GET request, this is the correct syntax:
$.ajax({
type: 'GET',
url: url,
success: function(response) {
alert(response);
}
});
You could also use $.get to accomplish the same thing:
$.get(url, function(response){
alert(response);
});

$.ajax({
type: "GET",
url: "/queryjob/dbname/ip",
success: function (data) {
alert(data);
}
});
Yes, it was a syntax error ;)

Related

ajax to an html site is getting 200 but the response is empty

An example:
this is the site I'm trying to get the HTML:
https://stackoverflow.com/questions/2294493/how-to-get-the-position-of-a-character-in-python
and the AJAX call
$.ajax({
url: href,
type: "get", //send it through get method
success: function(response) {
var htmlElem = $($.parseHTML(response));
console.log({response})
},
error: function(xhr) {
//Do Something to handle error
}
})
the response is empty ("") all the time, can someone know why? and how to fix it?
EDIT:
Loading cross-domain endpoint with jQuery AJAX
this is not fixinig the issue

JQuery Ajax petition is modifiying given URL

I want to consume a API Rest aplication using JQuery Ajax. this is the code that I have:
var res=$('#myForm').attr('action');
console.log(res);
$.ajax({
url: res,
success: function (data) {
alert('success!!');
},
dataType: 'html'
});
The console.log sentence is printing the url correctly, I just copied and pasted it into the browser and its correct, it's something like this:
http://localhost/myproject/public/2
But then, the request gives a 404 error, and the URL that is requesting is this one:
http://localhost/localhost/myproject/public/2
So, why it's attaching another localhost line to the url? I just don't understand!
All you need is to get the part after localhost. For this, please use split method.
var res=$('#myForm').attr('action');
console.log(res);
$.ajax({
url: res.split('localhost')[1],
success: function (data) {
alert('success!!');
},
dataType: 'html'
});

Call php function with jquery using ajax

I'm trying to call a php function that is defined on a php file.
On the php file I have this that will call the function
if(isset($_GET))
{
submit(); //Do something with your GET
}
But I'm getting an error when calling the php function within the jquery. I imagine it could be a path problem. The jquery file is in js/file.js and the php function is on data/php/function.php so I try it calling it like this.
$.ajax({
url: "../data/php/functions.php&paperid="+$('#table-paperid').text(),
type: "GET",
success:function(result)//we got the response
{
alert('Successfully called');
},
error:function(exception){alert('Exception:'+exception);}
});
But no matter what I write on the url I always get the exception error. Any ideas?
It's better practice to use a POST ajax call in this case. You should also put the "type" property before the "url" field, although, I doubt this is the source of the error. Try an ajax call that looks something like this.
$.ajax({
type: 'post',
url: '/some/path',
data: { paperid : $('#table-paperid').text()},
success: function(data) {
var response = JSON.parse(data);
}
})
and your php should be modified to.
if(isset($_POST['paperid']))
{
submit(); //Do something with your GET
}
I've done this many times with no issue so hopefully this will solve your problem.
Best of luck!
You can set the query string this way as well:
$.ajax({
url: "../data/php/functions.php",
type: "GET",
data: {paperid: $('#table-paperid').text() }
success:function(result)//we got the response
{
alert('Successfully called');
},
error:function(exception){alert('Exception:'+exception);}
});

ajax https request jsonp success callback not working

Im run this script on my http site, on network tab show what request 200 ok,
but success callback not runing
$.ajax({
url: 'https://seobudget.ru',
success: function(data){
alert("123")
},
crossDomain:true,
dataType:"jsonp"
});
im try remove "crossDomain:true" but its nothing change
$.ajax({
url: 'https://seobudget.ru',
dataType:"jsonp",
success: function(data){
alert("123")
}
});
you put dataType in wrong place

AJAX result OK but no data

I'm using jQuery to perform following AJAX request:
jQuery.ajax({
url: "url-to-script",
dataType: "html",
async: false,
success: function(data) {
console.log(data);
},
error:function(){
showMessage('Chyba', 'error');
}
});
Script that I'm calling is returning part of HTML. It is working properly.
But when I call this AJAX it will end with blank response in data. When I call it again it will return what it should and each next call is working. But the first one after page load is never working. I've no idea why.
I've also tried
if (data)
// do something
else
try again
But it just freeze my browser. So I don't know what is wrong.
Thanks for ideas!

Categories

Resources