The task is very simple I only need to save these json responses into my code to manipulate it later but I can't get to make it function. I've tried more than a strategy with nothing working. Thing is it's working for a json and not the other with the exact same syntax. Here is my code
function searchOwner() {
$.ajax({
type: "GET",
contentType: "application/json",
dataType: "jsonp",
url: "https://elvet.eu-gb.mybluemix.net/get_pet_owner?pet_id=p12345678912345",
success: function(responseData) {
alert(responseData)
}
})
}
function searchPet() {
$.ajax({
type: "GET",
contentType: "application/json",
dataType: 'jsonp',
url: "https://elvet.eu-gb.mybluemix.net/getpets?pet_id=p12345678912345",
success: function(responseData) {
alert('ok')
}
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
The SearchOwner is not working at all while searchPet is working just fine. Any help would be highly appreciated thank you
dataType : "json", in searchOwner() where as it's dataType: 'jsonp' in searchPet()
Related
i have issue to send data in flask from js. I try use ajax.
AJAX code
$.ajax({
type: "GET",
contentType: "application/json;charset=utf-8",
url: "/getmethod/data",
traditional: "true",
data: JSON.stringify({data}),
dataType: "json"
});
Python code
def get_javascript_data(data):
content = request.get_data('data')
jsonify(content)
If u got some tips or tricks please tell me.
new_item = $('#input').val() //value I want to send
$.ajax({
url: '/set_value',
type: 'POST',
data: new_item,
success: function(response){
$('#main').text(response)
}
})
in flask:
new_item = request.get_data()
I don't know what's wrong. I've spent almost an hour reading and re-reading, checking my spelling etc. I was hoping maybe someone can point out what I'm doing wrong.
This is the cURL statement that returns successfully in the terminal:
curl https://api.gumroad.com/v2/products \
-d "access_token=123456abcdef" \
-X GET
The following are some of my attempts that did not work. And yes, I'm certain jQuery has been loaded:
$.ajax({
url: "https://api.gumroad.com/v2/products",
data: "access_token=123456abcdef",
success: function(result){
console.log(result);
}});
And this one:
$.ajax({
url: "https://api.gumroad.com/v2/products",
data: "access_token=123456abcdef",
processData: false,
type: "get",
success: function(result){
console.log(result);
}});
And another one:
$.ajax({
url: url,
beforeSend: function(xhr) {
xhr.setRequestHeader("access_token", "123456abcdef")
}, success: function(data){
alert(data);
//process the JSON data etc
}
})
Looks like you are sending a string instead of a data object, try this:
$.ajax({
url: "https://api.gumroad.com/v2/products",
data: {access_token: "123456abcdef"},
processData: false,
type: "get",
success: function(result) {
console.log(result);
}
});
This is what I am trying to do. On a home page.. say /home.jsp, a user clicks on a link. I read value of the link and on the basis of which I call a RESTful resource which in turn manipulates database and returns a response. Interaction with REST as expected happens with use of JavaScript. I have been able to get information from REST resource but now I want to send that data to another JSP.. say /info.jsp. I am unable to do this.
I was trying to make another ajax call within success function of parent Ajax call but nothing is happening. For example:
function dealInfo(aparameter){
var requestData = {
"dataType": "json",
"type" : "GET",
"url" : REST resource URL+aparameter,
};
var request = $.ajax(requestData);
request.success(function(data){
alert(something from data); //this is a success
//I cannot get into the below AJAX call
$.ajax({
"type": "post",
"url": "info.jsp"
success: function(data){
alert("here");
("#someDiv").html(data[0].deviceModel);
}
});
How do I go about achieving this? Should I use some other approach rather than two Ajax calls? Any help is appreciated. Thank You.
You can use the following function:
function dealInfo(aparameter) {
$.ajax({
url: 'thePage.jsp',
type: "GET",
cache: false,
dataType: 'json',
data: {'aparameter': aparameter},
success: function (data) {
alert(data); //or you can use console.log(data);
$.ajax({
url: 'info.jsp',
type: "POST",
cache: false,
data: {'oldValorFromFirstAjaxCall': data},
success: function (info) {
alert(info); //or you can use console.log(info);
$("#someDiv").html(info);
}
});
}
});
}
Or make the AJAX call synchronous:
function dealInfo(aparameter) {
var request = $.ajax({
async: false, //It's very important
cache: false,
url: 'thePage.jsp',
type: "GET",
dataType: 'json',
data: {'aparameter': aparameter}
}).responseText;
$.ajax({
url: 'info.jsp',
type: "POST",
cache: false,
data: {'oldValorFromFirstAjaxCall': request},
success: function (info) {
alert(info); //or you can use console.log(info);
$("#someDiv").html(info);
}
});
}
In this way I'm using.
"type": "post" instead of type: 'post'
Maybe it will help. Try it please. For Example;
$.ajax({
url: "yourURL",
type: 'GET',
data: form_data,
success: function (data) {
...
}
});
I am using the below code snippet to fetch data from a certain webAPI.
data.responseText works fine in FireFox but gives undefined in IE L
I have tried using data.responseJSON also but it doesn’t work in IE.
Please give me the solution to this.
Here is the code which I am using.
$.ajax({
type: "GET",
url: serviceUrl,
contentType: "application/json",
data: "{'slid':'" + slidname + "'}",
async: false,
crossDomain: true,
complete: function (data) {
alert("hii");
alert(data.responseText);
}
});
Which version of the IE? If IE9 or less, the issue could be your jQuery version - see http://jquery.com/browser-support/.
Otherwise, you could try if letting the browser do the formatting of the ajax request data helps:
$.ajax({
type: "GET",
url: serviceUrl,
contentType: "application/json",
data:JSON.stringify( {"slid": slidname} ),
async: false,
crossDomain: true,
complete: function (data) {
alert("hii");
alert(data.responseText);
}
});
If this also fails, you may want to try "jsonp" as dataType for the ajax call since you have a cross-domain request.
$.ajax({
type: "GET",
url: serviceUrl,
contentType: "application/json",
data: JSON.stringify( {"slid": slidname} ),
async: false,
crossDomain: true,
dataType: "jsonp",
complete: function (data) {
alert("hii");
alert(data.responseText);
}
});
I try to understand one thing.
I want to post an object with jquery Ajax POST , something like this:
var dataPostYear = {
viewType:GetViewType(),
viewDate:'2009/09/08',
languageId:GetLanguageId()
};
$.ajax({
type: "POST",
url: url,
data: dataPostYear,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnLoadYearListSuccess,
error: OnLoadYearListError
});
and it doesn't work.
But this one works fine:
var dataPostYear = "{viewType:'"+ GetViewType() + "',viewDate:'2009/09/08',languageId:'"+GetLanguageId()+"}";
$.ajax({
type: "POST",
url: url,
data: dataPostYear,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnLoadYearListSuccess,
error: OnLoadYearListError
});
GetViewType() return --'0'
languageId() return --'1'
it's just a string
there is a way to post an object, something what I try to do in my first way ? Or not ?
Thanks
Use jQuery.param(). Here is the documentation
You should look at .postJSON.
Essentially, you just add json as a 4th argument to the $.post
From the site:
// Send the request
$.post('script.php', data, function(response) {
// Do something with the request
}, 'json');
If you want the .ajax call version, you can convert it using the .post docs.