Can someone tell me what is wrong this code?
$.ajax({
"url":"https://en.wikipedia.org/w/api.php?",
"dataType": "jsonp",
"action": "opensearch",
"format": "json",
"search": "new york",
"namespace": "0",
"limit": "3",
"formatversion": "1",
success: function(response){
console.log(response);
}
});
Why do I get the following error message?
Refused to execute script from 'https://en.wikipedia.org/w/api.php?&callback=jQuery111105448874468459555_1518288921946&_=1518288921947' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
Thank you.
The following are not ajax parameters (instead they are api parameters):
"action": "opensearch",
"format": "json",
"search": "new york",
"namespace": "0",
"limit": "3",
"formatversion": "1",
You can pass those values as query string, changing your url from:
"url":"https://en.wikipedia.org/w/api.php?",
to:
"url":"https://en.wikipedia.org/w/api.php?"+ $.param(apiParams),
where the api parameter is:
var apiParams = {action: 'opensearch', search: 'new york', limit: 3, namespace: 0, formatversion: 1, format: 'json'};
or you can use the data parameter like:
data: apiParams,
The snippet:
var apiParams = {action: 'opensearch', search: 'new york', limit: 3, namespace: 0, formatversion: 1, format: 'json'};
$.ajax({
"url":"https://en.wikipedia.org/w/api.php?" + $.param(apiParams),
"dataType": "jsonp",
//data: apiParams,
success: function(response){
console.log(JSON.stringify(response));
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Related
I am using codeigniter4.
In the current environment, the csrf token must be included in the post data when making an ajax request.
I understand adding get parameters, but is it possible to add post data?
//Build Tabulator
var table = new Tabulator("#example-table", {
height: "311px",
layout: "fitColumns",
ajaxURL: "user/test/ajaxtest/",
ajaxParams :{ key1 : "value1" , key2 : "value2" },
ajaxType:"POST",
progressiveLoad: "scroll",
paginationSize: 20,
placeholder: "No Data Set",
columns: [{
title: "Name",
field: "name",
sorter: "string",
width: 200
},
{
title: "Progress",
field: "progress",
sorter: "number",
formatter: "progress"
},
{
title: "Gender",
field: "gender",
sorter: "string"
},
{
title: "Rating",
field: "rating",
formatter: "star",
hozAlign: "center",
width: 100
},
{
title: "Favourite Color",
field: "col",
sorter: "string"
},
{
title: "Date Of Birth",
field: "dob",
sorter: "date",
hozAlign: "center"
},
{
title: "Driver",
field: "car",
hozAlign: "center",
formatter: "tickCross",
sorter: "boolean"
},
],
});
Anyone know please.
You could use ajaxRequestFunc to do this.
Refer to the documentation here. Using this, you could process your ajax request however you would prefer and just return the data to the table.
You could raise a POST request with params in your custom ajax call using ajaxRequestFunc
Try this.
$.ajax({
url: '/controller/method',
type: 'POST',
data: {
'csrf_test_name': '<?= csrf_token() ?>',
'param1': 'value1',
'param2': 'value2'
},
success: function(response) {
console.log(response);
}
});
In this example, csrf_token is the token you obtained from CodeIgniter's function, and 'param1' and 'param2' are the additional post data you want to send to the server.
You can access these post data in your CodeIgniter controller using the $this->request->getPost('param1') and $this->request->getPost('param2') functions. Don't forget to include the 'csrf_test_name' parameter in your post data to pass the CSRF token.
I am practising my ajax with a json file on local computer, using google chrome. the json file is par of sources when I inspect the website but i keep getting a parsererror
hears my js code
$(function(){
$.ajax({
type: 'GET',
url: 'sample3.json',
dataType: 'jsonp',
crossDomain: true,
cache: false,
async: true,
jsonp: false,
success: function(data) {
console.log('success', data);
},
error:function(e, msg){
console.log('error')
console.log(JSON.stringify(e) + " " + msg);
}
})
})
I keep getting {"readyState":4,"status":200,"statusText":"load"} parsererror
my json file
[
{
"id": 1,
"name": "Will",
"drink": "Americano w/ Creme"
},
{
"id": 2,
"name": "Laura",
"drink": "Vanilla Macchiato"
}
]
to see what I am trying to create
https://jsfiddle.net/Anaconda2468642/rskfwucp/3/
and I am also getting this error:
error Error: myCallback was not called
at Function.error (jquery.min.js:2:2616)
at e.converters.script json (jquery.min.js:2:84147)
at jquery.min.js:2:79470
at l (jquery.min.js:2:79587)
at HTMLScriptElement.i (jquery.min.js:2:83436)
at HTMLScriptElement.dispatch (jquery.min.js:2:43064)
at HTMLScriptElement.v.handle (jquery.min.js:2:41048)
I am trying to create a fusion table using the documentation provided by Google fusion table REST API https://developers.google.com/fusiontables/docs/v2/using.
I am sending an ajax request in the following format
$.ajax({
type: "POST",
crossDomain: true,
contentType: "application/json",
url: "https://www.googleapis.com/fusiontables/v2/tables",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", token);
},
data: {
access_token: token,
"name": "Insects",
"columns": [
{
"name": "Species",
"type": "STRING"
},
{
"name": "Elevation",
"type": "NUMBER"
},
{
"name": "Year",
"type": "DATETIME"
}
],
"description": "Insect Tracking Information.",
"isExportable": true
},
async: false,
success: function(result){
alert(result);
},
error: function(result) {
alert('error');
}
});
But I am getting a 401 error. I tried setting Bearer , token in the authorization header but still no success.Any pointers will be much appreciated.Thank you
I solved my issue with:
xhr.setRequestHeader("Authorization", "Bearer "+ token);
I have the following piece of JSON:
[
{
"number": "0",
"name": "Russell Westbrook",
"attemptedFG": [
{
"x": "333",
"y": "97",
"made": "true",
"assisted": "false"
},
{
"x": "571",
"y": "389",
"made": "true",
"assisted": "false"
}
],
"attemptedFT": [
{
"made": "true"
},
{
"made": "false"
}
],
"rebounds": "5",
"assists": "8",
"steals": "2",
"blocks": "1",
"turnovers": "3",
"fouls": "4"
}
]
and I'm trying to parse it with this AJAX call using ReactJS:
loadStatsFromServer: function() {
$.ajax({
url: this.props.url,
dataType: 'json',
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
}
All the values in the JSON are still stringified after the AJAX call succeeds. The server sends back a response header with 'Content-Type', 'application/json' so I'm not sure why it's not parsing.
Server-side call:
app.get('/stats.json', function(req, res) {
fs.readFile('stats.json', function(err, data) {
res.setHeader('Content-Type', 'application/json');
res.send(data);
});
});
Thanks a bunch.
When you send a stringified json object from server to client, you need to parse that object to readable javascript object. So, you need to parse json in success event of ajax request:
success: function(data) {
var result = $.parseJSON(data);
}
Now result is a javascript readable object.
I'm trying to work with the freebase API but am hitting a snag with the basic formatting for the query I want to build...
The commented out query below works fine... but not my attempt above. I'm sure the solution is simple but I haven't been able to work it out.
$.ajax({
url: "https://www.googleapis.com/freebase/v1/mqlread",
//url: "https://www.googleapis.com/freebase/v1/search",
dataType: "jsonp",
data: {
query: [{
"limit": 8,
"name": null,
"name~=": request.term+"*"
}]
//query: '[{"name":null,"name~=":"ambrose*","limit":8}]'
},
success: function( data ) {
response( $.map( data.result, function( item ) {
return {
label: item.name,
value: item.name
}
}));
}
});
Commented line value is string(between ' '):
//query: '[{"name":null,"name~=":"ambrose*","limit":8}]'
and here we have array:
query: [{
"limit": 8,
"name": null,
"name~=": request.term+"*"
}]
change to:
query: '[{"limit": 8,"name": null,"name~=": "'+request.term+'*"}]'
and now should work
UPDATE:
convert json to string:
JSON.stringify([{
"limit": 8,
"name": null,
"name~=": request.term+"*"
}])