jQuery的阿贾克斯在不同的端口 [英] jQuery Ajax on Different Port

查看:195
本文介绍了jQuery的阿贾克斯在不同的端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PHP文件。

我在8080端口上的index.html

My index.html on port 8080

$(document).ready(function(){
$.get("userCheck.php", 
        {"username" : "lazy", "favcolor" : "FFFFFF" },          
        function(data){ alert("Data Loaded: " + data);
});

我的PHP

My PHP

$user = $_GET["username"];
if($user == "lazy")
    echo "SUCESS";
else
    echo "FAIL";

我用Google搜索升技,JSONP出来居多。不知道如何将它转换为JSONP?

I have googled abit, JSONP came out mostly. Any idea how to convert it to JSONP?

什么办法,使工作?

推荐答案

实现一个JSONP服务非常简单,你只需要一个的回调的GET参数并在结束时,打印出含有相当于一个字符串与JSON数据作为参数的函数调用:

Implementing a JSONP service is really simple, you need only a callback GET parameter and at the end, print a string containing the equivalent to a function call with the JSON data as the argument:

$callback = $_GET["callback"];
$user = $_GET["username"];

if($user == "lazy") {
  $response = array("message" => "SUCESS");
} else {
  $response = array("message" => "FAIL");
}

echo $callback . "(". json_encode($response) . ");";

然后你可以使用它与jQuery $。的getJSON

$.getJSON("jsonpTest.php?callback=?", { username: "lazy"}, function(json){
  alert("JSON Data: " + json.message); // SUCCESS
});

这篇关于jQuery的阿贾克斯在不同的端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆