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.
精彩评论