I'm having some issues with Ajax POST and sending an array as a JSON.
I firstly get my data externally break it apart a little and then send it to my function.
const getPrices = async () => {
const now = Epoch(new Date());
var company1 = $('#fcomp').val();
var company2 = $('#lcomp2').val();
console.log("Company1 =", company1);
console.log("Company2 =", company2);
const response = await fetch("https://alpha-vantage.p.rapidapi.com/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol="+company1+"&outputsize=compact&datatype=json", {
"method": "GET",
"headers": {
"x-rapidapi-key": "f16d556786msh84b3a1e5cb78a33p172e62jsnb2827ccaab51",
"x-rapidapi-host": "alpha-vantage.p.rapidapi.com"
}
});
await delay(5000);
console.log("Waited 5s for API rules");
const response2 = await fetch("https://alpha-vantage.p.rapidapi.com/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol="+company2+"&outputsize=compact&datatype=json", {
"method": "GET",
"headers": {
"x-rapidapi-key": "f16d556786msh84b3a1e5cb78a33p172e62jsnb2827ccaab51",
"x-rapidapi-host": "alpha-vantage.p.rapidapi.com"
}
});
const Prices = await response.json();
const Prices2 = await response2.json();
const just_prices = Prices["Time Series (Daily)"];
const just_prices2 = Prices2["Time Series (Daily)"];
sendToPHP(company1,just_prices);
sendToPHP(company2,just_prices2);
this then sends (Or Should Send) the company code and the array
function sendToPHP (company,json){
console.log("Attempting POST" );
$.ajax({
type : "POST", //type of method
url : "daily_push.php", //your page
dataType: 'json',
data : { company : company, json : json },// passing the values
success: function(res){
console.log("Successful POST Response:", res );
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("Error Occured in POST: ", errorThrown);
console.log("Error Occured in POST: ", textStatus);
console.log("Error Occured in POST: ", XMLHttpRequest);
console.log("JSON: ", json);
}
});
}
im Debugging in Chrome Dev and am getting this responce: "SyntaxError: Unexpected token N in JSON at position 0"
I dump my Data Afterwards and it all Looks OK??
Im a little stumped now after searching around for an hour or so....
If I have missed something glaringly obvious I can only appologies for wasting your time!
EDIT 1: More info (Prices Ouput) here is the output from Prices:
[ ] Still not sure what is going on with it? hey all seem to be Objects. ONly thing I can think is that I need to maybe unpack each indevidual object thats held in tha dates then pass it back?
The issue I was having is I was Sending dataType: 'json',
whereas it should have been dataType: 'html',
This then gets recieved by the PHP script and Successfully responds. See image.
$.ajax({
type : "POST", //type of method
url : "daily_push.php", //your page
dataType: 'html',
data : { company : company, json : json },// passing the values
success: function(res){
console.log("Successful POST Response:", res );
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("Error Occured in POST: ", errorThrown);
console.log("Error Occured in POST: ", textStatus);
console.log("Error Occured in POST: ", XMLHttpRequest);
console.log("JSON: ", json);
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.