I am trying to make the simplest API between one site and another, try to implement the simplest option, namely, just send a request to the php page of the site and get back the data that it will leave.

on the server there is such php file:

$v['id'] ='11'; $v['nn']='22'; echo json_encode($v); 

just generates a test string in jason

and this is how I try to send a request for receiving data from this file.

  jQuery.ajax({ url: "http://www.****.com/hyst/acore.php", type: "GET", contentType: 'application/json; charset=utf-8', success: function(resultData) { alert(resultData); }, error : function(jqXHR, textStatus, errorThrown) { alert(textStatus); } }); 

but unfortunately, I get an error, where was I wrong? Is it appropriate to use Ajax here if the domains of the site with the API and the site that sends the request are different? if not ayaks then how best to do?

  • if an ERROR pops up (you know how), then nothing will help. - n.osennij
  • What error occurs? - XelaNimed
  • @XelaNimed textStatus when an alert is displayed, ohm gives an error, but if you look in the console, then everything is fine, the request goes away and the answer is ibb.co/whdcfKQ with all the necessary parameters, so the script itself is probably not the right thing - dantelol February
  • And why type of request HTML, but not JSON? What does the console show in textStatus or resultData? - XelaNimed
  • Header header('Content-type: application/json'); on PHP server send to browser / client? - XelaNimed 1:54 pm

2 answers 2

The solution is to create another PHP file — the JS bridge accesses the PHP bridge and the PHP bridge addresses the external resource using the “include (resource_URL)” tool, then it returns the data you need ...

  • Yes, I think this would be suitable, but I’m having an application that unfortunately cannot use php files. This is a js application that is then mounted in apk format \ dantelol
  • Try removing: contentType: 'application / json; charset = utf-8 ', - Adrian Golub
  • Unfortunately, there is still an error in textStatus (even though the console shows that the site gives an answer! ibb.co/whdcfKQ - dantelol February

Understood! In general, maybe someone will need a solution like that? In general, the fact was that in the php handler there was no line

  header('Access-Control-Allow-Origin: *'); 

in the end, my handler (acore.php) began to look like this

  header('Access-Control-Allow-Origin: *'); header("Content-type: application/json; charset: utf-8");; $v['id'] ='11'; $v['nn']='22'; echo json_encode($v); 

Well, the query decided to do this:

  fetch('http://www.****.com/hyst/acore.php') .then(function(response) { //alert(response.headers.get('Content-Type')); // application/json; charset=utf-8 //alert(response.status); // 200 return response.json(); }) .then(function(data) { alert(data.id); // тут выходит наши данные записанные под id }) .catch( alert ); 

although it is possible and so to call

  var request = new XMLHttpRequest(); request.open('GET', 'http://www.****.com/hyst/acore.php', false); request.onload = function() { if (request.status >= 200 && request.status < 400) { var data = JSON.parse(request.responseText); alert(data.id); } else { alert('error'); } }; request.send(); 
  • one
    Maybe it makes sense to limit the site ( Access-Control-Allow-Origin: http://www.****.com ), and not to allow ( Access-Control-Allow-Origin: * ) all to perform requests to your API? - XelaNimed
  • @XelaNimed yes I agree, I'm just new to this) but I have another problem there now when I test through the browser - the entire connection request / response happens fine, but when I mount it via cordova in the apk file and run it on the phone, the script issues an error 404 and now I can’t understand - it’s already in the application itself that it’s necessary to expose what it would be allowed to send http requests - dantelol February
  • In my opinion, this is another question, but look in the direction of debugging Cordova on Visual Studio or another IDE docs.microsoft.com/en-us/visualstudio/cross-platform/… - XelaNimed