发送消息从php发送到ajax,'echo json_encode'无效

时间:2015-11-12 01:42:04

标签: javascript php jquery ajax

我正在尝试从php向ajax发送消息。我正在使用public static final int EARTH_RADIUS = 3959; // Move constants out of methods public ZipCode findZip(int zip){ for(ZipCode zipCode : myZips){ if(zipCode.getZipCode() == zip){ return zipCode; } } return null; } public int distance(int zip1, int zip2){ // save these references so you can use them later ZipCode z1 = findZip(zip1); ZipCode z2 = findZip(zip2); if(z1 != null && z2 != null) { double lat1 = Math.toRadians(z1.getLatitude()); double long1 = Math.toRadians(z1.getLongitude()); double lat2 = Math.toRadians(z2.getLatitude()); double long2 = Math.toRadians(z2.getLongitude()); double p1 = Math.cos(lat1) * Math.cos(long1) * Math.cos(lat2) * Math.cos(long2); double p2 = Math.cos(lat1) * Math.sin(long1) * Math.cos(lat2) * Math.sin(long2); double p3 = Math.sin(lat1) * Math.sin(lat2); double distance = Math.acos(p1+p2+p3) * EARTH_RADIUS; return (int)Math.round(distance); } else return -1; } public ArrayList <ZipCode> withinRadius(int pZip, int pRadius){ ZipCode zip = findZip(pZip); // save this reference to calculate the distances ArrayList<ZipCode> zips = new ArrayList<ZipCode> (); if (zip != null) { for (int i = 0; i < myZips.size(); i++) { ZipCode z = myZips.get(i); // or use a for-each loop like in findZip if (distance(zip.getZipCode(), z.getZipCode()) <= pRadius) { zips.add(z); } } } return zips; } 来执行此操作。当我这样做时,网站会显示echo json_encode消息。 (arrays)。如何才能将显示消息?

此外,来自ajax的警报不会被调用。

以下是代码:

{"foo":"content of foo"}

如何让阵列无法在网站上显示?为什么ajax if语句没有被调用?

1 个答案:

答案 0 :(得分:0)

您需要在脚本顶部设置HTTP标头,以便返回子:

header('Content-type: application/json');

你的脚本有点令人困惑。你不能返回json和html / javascript。

它是一个或另一个。