简体   繁体   English

Ajax调用php文件中的外部json对象

[英]Ajax call on an external json object within a php file

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). 我正在尝试使用来自不同服务器(技术上是相同的提供程序,但我认为并不重要)的ajax解析外部php文件中的json文件。

Anyway my code in my wordpress website to create a json file from my database is the following: 无论如何,我在wordpress网站上从数据库创建json文件的代码如下:

        <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 . 该文件的URL是(例如) http://somewebsite.com/phpjson.php

I'm calling from the other url (example) http://app.someotherwebsite.com in this way: 我以这种方式从其他网址(例如) http://app.someotherwebsite.com拨打电话:

$(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: 几个问题:

  1. The result of json_encode() is a string, not an array. json_encode()的结果是一个字符串,而不是一个数组。 So instead of using print_r() you should be using echo or print . 因此,应该使用echoprint而不是使用print_r()
  2. Wrapping the JSON in <pre> tags invalidates the JSON, so it can't be parsed by jQuery. 将JSON包装在<pre>标记中会使JSON无效,因此jQuery无法对其进行解析。 You should remove the <pre> / </pre> tags. 您应该删除<pre> / </pre>标记。
  3. For best results, make sure you set a content-type header before outputting the JSON data: header('Content-Type: application/json'); 为了获得最佳结果,请确保在输出JSON数据之前设置了内容类型标头: header('Content-Type: application/json'); .

Try that and let us know if it solves the problem. 试试看,让我们知道它是否可以解决问题。

Check documentation : http://api.jquery.com/jQuery.ajax/ 检查文档: http : //api.jquery.com/jQuery.ajax/

crossDomain (default: false for same-domain requests, true for cross-domain requests) crossDomain(默认值:对于同域请求为false,对于跨域请求为true)

Type: Boolean 类型:布尔

If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. 如果您希望在同一域上强制执行crossDomain请求(例如JSONP),请将crossDomain的值设置为true。 This allows, for example, server-side redirection to another domain. 例如,这允许将服务器端重定向到另一个域。 (version added: 1.5) (添加的版本:1.5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM