Jquery Ajax Post Issue - ajax

I am trying to send some information to my php page via an ajax call. However I am getting an issue that a } missing after property list. Below is my code:
function article(Article){
var surl = "http://www.webapp-testing.com/includes/article_desc.php";
var id = 1;
$.ajax({
type: "POST",
url: surl,
data: '"Article="+Article';
dataType: "jsonp",
cache : false,
jsonp : "onJSONPLoad",
jsonpCallback: "articlecallback",
crossDomain: "true",
success: function(response) {
alert("Success");
},
error: function (xhr, status) {
alert('Unknown error ' + status);
}
});
}
Any help will be greatly appreciated

Change the ; after your data attribute to a ,.
data: '"Article="+Article',

This string should end with ,:
data: '"Article="+Article';

You have a semicolon after the "data" element. It should be a comma.

replace the the semicolon on line data: '"Article="+Article'; with ,

Related

Variable loses some value when used in Ajax API Call

I have an issue that causes my variable “tempid” to lose some of its values when put into the second API call. As you can see from my image, if I log the variable to console (console.log(tempid)) it shows just fine. However, as soon as I place it in an API call it has some of the value but not all. Could you please help me by explaining why this would happen?
[console example][1]
$(document).ready(function() {
$.ajax({
url: "/api/Template/GetTemplates?classId=7ac62bd4-8fce-a150-3b40-16a39a61383d",
async:true,
dataType: 'json',
success: function(data) {
$(data).each(function (data) {
if (this.Name === "Name of Template"){
var tempid = this.Id
console.log (tempid)
var tempurl = "/api/V3/Projection/CreateProjectionByTemplate?id=" + tempid + "&createdById=703853d4-ffc4-fce3-3034-0b838d40c385"
$.ajax({
url: tempurl,
async: false,
dataType: 'json',
success: function(data) {
}
});
}
});
}
});
})
[1]: https://i.stack.imgur.com/gyesK.png
I found the answer, the console is just showing a shortened version of the URL and happened to cut off part of the tempid. Thank you

Pass Parameter in Ajax URL

I am trying to pass parameter in below ajax url
function endprogress(){
$("#surfbar").html("Please wait...");
var dataString = 'action=validate&t='+adtk;
$.ajax({
type: "POST",
url: 'yes_manage.php?view=surf&track='+user_name&, /*this line giving error*/
data: dataString,
success: function(msg){
}
});
}
above url field is giving error Expected and identifier instead saw ','
how can I resolve this
Change user_name& => user_name
look like
function endprogress(){
$("#surfbar").html("Please wait...");
var dataString = 'action=validate&t='+adtk;
$.ajax({
type: "POST",
url: 'yes_manage.php?view=surf&track='+user_name, /*remove & at the end user_name*/
data: dataString,
success: function(msg){
}
});
}

Weird object returned from AJAX request

I have this method:
var chineseCurrency = getChinese();
function getChinese(){
return $.ajax({
context: this,
type: 'GET',
dataType: 'json',
url: "https://www.cryptonator.com/api/ticker/usd-cny"
});
}
That is what printed when console.log(chineseCurrency);:
I am not able to make chineseCurrency equal to "price", so it would be "6.80071377". How can I do that? Tried chineseCurrency.responseText, nope, chineseCurrency['responseText'], nope. Tried to JSON.parse(chineseCurrency), nope. Nothing works!
Sorry if repeated, couldn't find any answer at Stackoverflow.
How do I return the response from an asynchronous call?
Data that is received as response to asynchronous ajax call cannot be returned from the function that calls $.ajax. What you are returning is XMLHttpRequest object (see http://api.jquery.com/jquery.ajax/) that is far from the desired data.
var chineseCurrency = null;
function getChinese(){
return $.ajax({
context: this,
type: 'GET',
dataType: 'json',
url: "https://www.cryptonator.com/api/ticker/usd-cny",
success: function(data) {
alert("success1: chineseCurrency=" + chineseCurrency);
chineseCurrency = data.ticker.price;
alert("success2: chineseCurrency=" + chineseCurrency);
// do what you need with chineseCurrency
}
});
}
You are not taking the data from that is returned from the Ajax call. instead you are just returning the ajax object.
Change your code to :
$.ajax(
{
context: this,
type: 'GET',
dataType: 'json',
url: "https://www.cryptonator.com/api/ticker/usd-cny"
data :{},
error : function(data)
{
console.log('error occured when trying to find the from city');
},
success : function(data)
{
console.log(data); //This is what you should return from the function.
}
});

Ajax Success Function not working

I am using Ajax to add contents on my database. And here's the code:
function addToFavorites(){
var recipe_id = $("#recipe_id").val();
var url = connect_url+'addFavorite.php?user_id='+user_id+'&recipe_id='+recipe_id;
$.ajax({
type: 'POST',
data: [{
user_id: user_id,
recipe_id: recipe_id
}],
url: url,
async: true,
jsonpCallback: 'userCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function (data) {
alert("HELLO!!!");
},
error: function (e) {
alert("ERROR!");
}
});
}
The Ajax call was successful and I was able to add records on the database but I'm just wondering why is it not displaying the alert message if the calling was successful? Is there something wrong with my code? Or is there something wrong with my understanding? Thanks!
you must give a response with some info to the ajax or it won't know the response succeeded

ajax post getting value in CLASSIC asp of html

I am trying to get the value i am sending from one page to another using jquery ajax json.
This is my code:
function checkTheVin()
{
$.ajax({
type: "POST",
url: "checkVin.asp",
data: 'theVIN=' + $("#vwVin").val(),
cache: false,
dataType: "html",
beforeSend: function() {$.blockUI({ message: '<img src="img/ajax-loader.gif" />' });},
complete: function(){$.unblockUI();},
success: function(responseText){
if (responseText.indexOf("GOOD") > -1)
{
$("#theContent").html(responseText.replace("GOOD",""));
}else{
//alert(data);
}
},
error: function(responseText){alert('err: ' + responseText);},
});
}
However i never get a resonse back. It's null.
This is how i am getting it using CLASSIC asp:
dim vwVin
vwVin = request.QueryString("theVIN")
What am i doing incorrect?
David
Try using a GET method in your ajax call:
$.ajax({
type: "GET",
url: "checkVin.asp",
data: 'theVIN=' + $("#vwVin").val(),
cache: false,
dataType: "html",
beforeSend: function() {$.blockUI({ message: '<img src="img/ajax-loader.gif" />' });},
complete: function(){$.unblockUI();},
success: function(responseText){
if (responseText.indexOf("GOOD") > -1)
{
$("#theContent").html(responseText.replace("GOOD",""));
}else{
//alert(data);
}
},
error: function(responseText){alert('err: ' + responseText);},
});
You have three options
1. Use the Request Object with out specifying the collection
dim vwVin
vwVin = request("theVIN")
The web server will then search request collections for you, first the querystring and then the form.
2. Specify the Request.Form collection if you are using Ajax Post
$.ajax({ type: "POST", ...
dim vwVin
vwVin = Request.Form("theVIN")
3. Specify the Request.QueryString collection if you are using Ajax GET
$.ajax({ type: "GET", ...
dim vwVin
vwVin = Request.QueryString ("theVIN")

Resources