开发者

jquery $.post returned data from php is empty

开发者 https://www.devze.com 2023-01-02 22:23 出处:网络
i\'m using this code to post to a php page: var qreq = \".....myurl.php\"; $.post(qreq, function(data){alert(data);});

i'm using this code to post to a php page:

var qreq = ".....myurl.php";
$.post(qreq, function(data){alert(data);});

in my PHP file i have this:

......
$prevtopic = $row["topic_id"];
echo $prevtopic;

the alert comes up, but is blank. for some reason data is empty even though i'm echoing from the PHP file. i also tried just echoing "hi" but that didn't work eith开发者_运维知识库er. what am i doing wrong?

thanks.


You cannot make a request cross-domain like this, what's blocking you is called the same-origin policy, in place to prevent malicious cross-domain requests (even if your's doesn't have evil intent, it's still blocked).

You can use JSONP for this situation, but not get normal html/text content as you have it. Here's a short writeup with a better description on JSONP, or here for a full example but you need to return JSONP from PHP.

As for the why? It's because http://evil.com/ shouldn't be able to make requests to http://mybank.com without some checks in place, that's why the policy exists.

0

精彩评论

暂无评论...
验证码 换一张
取 消