I'm trying to parse a json file within an external php file with ajax from a different server (technically the same provider but I don't think it really matters).
Anyway my code in my wordpress website to create a json file from my database is the following:
<pre>
<?php
global $wpdb;
if(!isset($wpdb))
{
require_once('wp-config.php');
require_once('wp-includes/wp-db.php');
}
$result = $wpdb->get_results ( "SELECT * FROM " . $table_prefix . "some_row" );
print_r(json_encode($result, JSON_UNESCAPED_SLASHES));
?>
</pre>
The url of this file is (example) http://somewebsite.com/phpjson.php .
I'm calling from the other url (example) http://app.someotherwebsite.com in this way:
$(document).ready(function() {
$(function(){
$.ajax({
url: "http://somewebsite.com/phpjson.php",
type: "GET",
dataType: "JSON",
cache: false,
success: function(markers) {
$.each(markers,function(i, val){
//do something
}
});
});
For some reason, I'm not able to call the file probably because of the format of for something else, any clue?
Several issues:
json_encode()
is a string, not an array. So instead of using print_r()
you should be using echo
or print
. <pre>
tags invalidates the JSON, so it can't be parsed by jQuery. You should remove the <pre>
/ </pre>
tags. header('Content-Type: application/json');
. Try that and let us know if it solves the problem.
Check documentation : http://api.jquery.com/jQuery.ajax/
crossDomain (default: false for same-domain requests, true for cross-domain requests)
Type: Boolean
If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain. (version added: 1.5)
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.