Javascript:如何在本地主机上测试JSONP

Javascript:如何在本地主机上测试JSONP,javascript,jsonp,Javascript,Jsonp,下面是我测试JSONP的方法:我运行XAMPP,并在文件夹htdocs中创建一个javascript文件夹。我创建了json1.json文件,其中包含一些要处理的数据 <head> <script> function updateSales(sales) { alert('test jsonp'); // real code here }

下面是我测试JSONP的方法:我运行XAMPP,并在文件夹htdocs中创建一个javascript文件夹。我创建了
json1.json
文件,其中包含一些要处理的数据

<head>
    <script>
            function updateSales(sales) {
                alert('test jsonp');
                // real code here
                }

    </script>

    </head>
    <body>
        <h1>JSONP Example</h1>
        <script src="http://localhost:85/javascript/json1.json?callback=updateSales"></script>
    </body>
之后,我在本地运行这个html文件,它将调用“其他机器”中的一个函数(在本例中是
localhost

<head>
    <script>
            function updateSales(sales) {
                alert('test jsonp');
                // real code here
                }

    </script>

    </head>
    <body>
        <h1>JSONP Example</h1>
        <script src="http://localhost:85/javascript/json1.json?callback=updateSales"></script>
    </body>
下面是我在这个问题上的代码:

<head>
    <script>
            function updateSales(sales) {
                alert('test jsonp');
                // real code here
                }

    </script>

    </head>
    <body>
        <h1>JSONP Example</h1>
        <script src="http://localhost:85/javascript/json1.json?callback=updateSales"></script>
    </body>

功能更新销售(销售){
警报(“测试jsonp”);
//这里是真代码
}
JSONP示例
但当我跑的时候,什么也没发生。但是,如果在互联网上改变为其他真正的json,它将起作用。意思是换行:

<head>
    <script>
            function updateSales(sales) {
                alert('test jsonp');
                // real code here
                }

    </script>

    </head>
    <body>
        <h1>JSONP Example</h1>
        <script src="http://localhost:85/javascript/json1.json?callback=updateSales"></script>
    </body>
<script src="http://localhost:85/javascript/json1.json?callback=updateSales"></script>

行:

<head>
    <script>
            function updateSales(sales) {
                alert('test jsonp');
                // real code here
                }

    </script>

    </head>
    <body>
        <h1>JSONP Example</h1>
        <script src="http://localhost:85/javascript/json1.json?callback=updateSales"></script>
    </body>
<script src="http://gumball.wickedlysmart.com/?callback=updateSales"></script>

它将顺利运行

<head>
    <script>
            function updateSales(sales) {
                alert('test jsonp');
                // real code here
                }

    </script>

    </head>
    <body>
        <h1>JSONP Example</h1>
        <script src="http://localhost:85/javascript/json1.json?callback=updateSales"></script>
    </body>
所以,我不知道如何使用localhost测试
JSONP
,请帮助我

<head>
    <script>
            function updateSales(sales) {
                alert('test jsonp');
                // real code here
                }

    </script>

    </head>
    <body>
        <h1>JSONP Example</h1>
        <script src="http://localhost:85/javascript/json1.json?callback=updateSales"></script>
    </body>

谢谢:)

JSONP是在服务器端生成的。服务器获取您的数据,将其编码为JSON,然后围绕它包装一个函数调用

<head>
    <script>
            function updateSales(sales) {
                alert('test jsonp');
                // real code here
                }

    </script>

    </head>
    <body>
        <h1>JSONP Example</h1>
        <script src="http://localhost:85/javascript/json1.json?callback=updateSales"></script>
    </body>
因此,在您的情况下,您可以通过以下方式修改
.json
文件:

<head>
    <script>
            function updateSales(sales) {
                alert('test jsonp');
                // real code here
                }

    </script>

    </head>
    <body>
        <h1>JSONP Example</h1>
        <script src="http://localhost:85/javascript/json1.json?callback=updateSales"></script>
    </body>
updateSales( { /* your data */ } );
然而,这将有一个硬编码的回调方法,所以它不会对您在检索时所做的更改做出反应

<head>
    <script>
            function updateSales(sales) {
                alert('test jsonp');
                // real code here
                }

    </script>

    </head>
    <body>
        <h1>JSONP Example</h1>
        <script src="http://localhost:85/javascript/json1.json?callback=updateSales"></script>
    </body>
另一种方法是编写一个小的PHP包装器(或者使用任何您喜欢的东西),在将JSON发送回客户端之前,在JSON周围添加函数

<head>
    <script>
            function updateSales(sales) {
                alert('test jsonp');
                // real code here
                }

    </script>

    </head>
    <body>
        <h1>JSONP Example</h1>
        <script src="http://localhost:85/javascript/json1.json?callback=updateSales"></script>
    </body>
<?php

  $json = file( "path/to/your/json" );

  echo $_GET['callback'], '(', implode( '', $json ), ');';

?>

(后面的代码决不是用于任何生产性用途,只是用于本地测试!)

<head>
    <script>
            function updateSales(sales) {
                alert('test jsonp');
                // real code here
                }

    </script>

    </head>
    <body>
        <h1>JSONP Example</h1>
        <script src="http://localhost:85/javascript/json1.json?callback=updateSales"></script>
    </body>