I'm trying to pass a JavaScript array into a PHP script to then be put into a database but I'm having issues. I have used JSON.stringify on the array before sending. The request seems to work in that when stepping through the code debugging the php page is called but no data is passed into the POST global. I'm sure its something amateurish that I've missed but I'm struggling. This is the code:-
$.ajax({
type: "POST",
datatype: "json",
url: "processdraw.php",
data: {
json: pDrawnTeams
},
contentType: "application/json; charset=utf-8",
success: alert('worked')
})
If you are not getting any error in your javascript, make sure that you are getting that parameter like this in your php target file:
$myJSON = $_POST['json']; // ['parameter name']
after that you will need to decode that json
$myData = json_decode($myJSON, true) // true is for retrieving as an array
Related
I am trying to write a script that gets COVID-19 numbers from the Ontario Database, but I keep getting the following error:
"invalid value \"_\""
Upon further investigation, the URL that is trying to be accessed contains the following parameter:
&_=1610496351832
As you can see in my code below, I never define such a variable:
var data = {
resource_id: '8a89caa9-511c-4568-af89-7f2174b4378c' // the resource id
};
$.ajax({
dataType: 'jsonp',
data: data,
url: 'https://data.ontario.ca/api/3/action/datastore_search'
});
Is there any way I can remove the _ object from the request?
The file I am trying to access is located at the following URL, where data is the resource_id.
https://data.ontario.ca/api/3/action/datastore_search?resource_id=8a89caa9-511c-4568-af89-7f2174b4378c
That is an automatic value generated by JQuery to avoid the cache.
Add the following to your script if you want to get rid of the underscore param:
var data = {
resource_id: '8a89caa9-511c-4568-af89-7f2174b4378c' // the resource id
};
$.ajax({
dataType: 'jsonp',
data: data,
type:'GET',
cache: true,
url: 'https://data.ontario.ca/api/3/action/datastore_search'
});
It only occurs in GET requests, you can play around with this setting. But remember, the automatic underscore param is included intentionally to avoid the request cashing.
I have a web page (written in php / codeigniter) that uses some javascript code.
I tried to move some javascript code to external file to better manage the code.
So in document ready event I load external file with
$.getScript('<?php echo base_url();?>assets/js/offerta_editScontirow.js');
one piece of code, putted in an external js file, does not work; instead it works correctly if I put all the code inside the main php file.
$.ajax({
dataType: 'json',
url: '<?php echo site_url("Offerta/applica_sconti");?>',
cache: false,
data: data,
success: function (data, status, xhr)
{
........
It does not work because it does not elaborate the php code, and in the query string I find
"php echo site_url("Offerta/applica_sconti");?>:"
So, is there a way to have it works? May I pass any parameter to the external js file while loading it, and pass the url to be used in ajax call? Some other method?
Kind regards,
Matt
try this window.my_url = 'some value'; and get the variable like below
window.my_url = '<?php echo site_url("Offerta/applica_sconti");?>';
$.getScript('<?php echo base_url();?>assets/js/offerta_editScontirow.js');
and
$.ajax({
dataType: 'json',
url: my_url,
cache: false,
data: data,
success: function (data, status, xhr)
{
I am not able to get data that is in Bengali language from server using ajax. The data from server is getting replaced by something I don't know. But if I normally set the data means not using ajax, data is getting displayed properly. My code is:
<meta content="html/text" charset="UTF-8"> in .jsp file
$.ajax({
url: "",
data: "a=a",
contentType: "charset=utf-8",
method: "get",
success:function(){
},
failure:function(){
}
});
I have taken reference from UTF8 encoding not working when using ajax, but even this did not work.
The error is in the ajax statement config.
contentType="charset=utf-8",
Here, you should use : instead of =.
See the error highlighted in the code below.
$.ajax({
url: "YOUR URL HERE?myparam1=value1¶m2=value2",
data: "a=a",
contentType: "charset=utf-8",
// ^
method: "GET",
success: function(resp) {},
failure: function(err) {}
});
I have the following Code in Json :
$.ajax({
type: "POST",
data: JSON.stringify(params),
contentType: "application/json; charset=utf-8",
url: AppCOJS.getBaseURL() + '/hcis/license-services/SE.TCC.CLU.Services.Transactional.ServiceManagementRequestInfoDetails.svc/SaveCustomerClearanceInformation',
dataType: "json",
async: true,
complete: SaveCustomerClearanceInformationCompleted
});
That code for adding values to table in DataBase sql server ,I submitte the request and I see the result data which is returned by WCF service in google chrome debugging window .
But when i go to the table i didn't see any values added to it ?
When I execute the request by Console Application ,It is working fine and the values are added to the table .
What is the problem ?
Being a newbie and working on a web application i am stuck at passing a JSON object in Javascript file to a SQL database. I have no clue on how to proceed further. I have created a SQL table with this addon of mozilla https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
Any suggestions on how to perform this?
If you want to pass JSON object to database then you can use "$.ajax".
Sample Code:
$.ajax({
type: "POST",
url: "mypage.aspx/mystaticmethod",
data: "{'fistcolumn':'firstvalue','secondcolumn':'secondvalue'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
});
}
});
At:
data: "{'fistcolumn':'firstvalue','secondcolumn':'secondvalue'}",
you can directly send your JSON object and at code behind you will get all values.
protected static void mystaticmethod(string firstcolumn, string secondcolumn)