0
<script>
window.addEventListener('load',function(){
var unique_code="3412313ad"// Initialize it with the unique code provided to you.
var param1="1"; // Initialize this with the value that you wish to see.For example 1 for navbar display , 2 for the side floating pop up
//while 3 for a transparent overlay on the whole page.
var domain=window.location.hostname;// current domain.
function jsonp(url, callback) {
var callbackName = 'jsonp_callback_' + Math.round(100000 * Math.random());
window[callbackName] = function(data) {
delete window[callbackName];
document.body.removeChild(script);
callback(data);
};
var script = document.createElement('script');
script.src = url + (url.indexOf('?') >= 0 ? '&' : '?') + 'callback=' + callbackName;
document.body.appendChild(script);
script.onerror=function(){
alert("failed to load snippet!");
}
}
jsonp('http://localhost/server.php?unique_code='+unique_code+'&domain='+domain, function(data) {
alert(data);
if(data.status=='success'){
alert('success');
}else alert(data.reason);
});
});
</script>
これは、jqueryのjsonpを模倣してリモートサーバーからスクリプトを取得するコードです。javascriptでjsonpを模倣する
:
は私がJavaScript XMLHttpRequest using JsonP
サーバー側のコードが
if(isset($_GET['unique_code']) && !empty($_GET['unique_code']) && isset($_GET['domain']) && !empty($_GET['domain'])){
$unique_code=$_GET['unique_code'];
$domain=$_GET['domain'];
$statement=$mysqli->prepare('select * from `snippet_users` where unique_code=? AND domain=?');
$statement->bind_param('ss',$unique_code,$domain);
if(!$statement->execute())
die(json_encode(array('status'=>'error','reason'=>'Server error.')));
$result=$statement->get_result();
if(mysqli_num_rows($result)>0)
die (json_encode(array('status'=>'success')));
else die(json_encode(array('status'=>'error','reason'=>'Unique code/Domain error.')));
}else{
die(json_encode(array('status'=>'error','reason'=>'Unique code/Domain error.')));
}
すべては完全に正常に動作しているだろうが、私は多少このように、コンソールにエラーが表示この質問に与えられた答えを使用しました
このエラーが発生しないように、私の解決策は何でしょうか。アラートボックス?
あなたがコードか何かでそれを編集するのでしょうか? –
urlで送信するコールバックパラメータは、ajaxスクリプトの出力、つまり 'jsonp_callback_123123123({status: 'error'、理由: 'wtf'})'で使用する必要があります。さもなければ、ブラウザはオブジェクトで何をすべきかを知らない。 –
これは私が探していたものです、ありがとう。 –