-1

Excuse me, I try to catch my guest and save it using javascript ajax post..

It is different host (between the js and php server). This is the javascript:

<script language="Javascript" src="http://www.codehelper.io/api/ips/?js"></script>
<script language="Javascript">
    var myip = codehelper_ip.IP;
    var mycountry = codehelper_ip.Country;
    var myurl = document.URL;
    $.ajax({
        type: 'POST',
        url: 'http://myhost.com/guest-catcher/guest-post.php',
        crossDomain: true,
        data: '{"ip":"'+myip+'", "country":"'+mycountry+'", "page":"'+myurl+'"}',
        dataType: 'json',
        success: function(responseData, textStatus, jqXHR) {
            alert("success");
        },
        error: function (xhr, status, error) {
            alert(xhr.responseText);
        }
    });
</script> 

And the server side code:

<?php

$ip = "";
if ($_POST['ip']) {
    $ip = $_POST['ip'];
}
$country = "";
if ($_POST['country']) {
    $country = $_POST['country'];
}
$page = "";
if ($_POST['page']) {
    $page = $_POST['page'];
}

//Start Save DB
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
$sql = "INSERT INTO blog_guest (ip_address, country, page) VALUES ('" . $ip . "', '" . $country . "','" . $page . "')";
if ($ip != "" || $country != "" || $page!= "") {
    if ($conn->query($sql) === TRUE) {
    } else {
        echo '<script>alert("' . $conn->error . '")</script>';
    }
}
$conn->close();
//End Save DB

Error at Browser console:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at myhost.com/guest-catcher/guest-post.php. This can be fixed by moving the resource to the same domain or enabling CORS.
joan16v
  • 4,551
  • 2
  • 44
  • 45
toha
  • 4,097
  • 3
  • 31
  • 49
  • You aren't returning any JSON from your server – A. Wolff Nov 20 '14 at 11:40
  • Just try to decode the json data in php file using this function. json_decode(); – Lakhan Nov 20 '14 at 11:56
  • It is different host..Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://myhost.com/guest-catcher/guest-post.php. This can be fixed by moving the resource to the same domain or enabling CORS. – toha Nov 20 '14 at 16:16
  • What is the error? – chameleon Sep 08 '16 at 03:21

4 Answers4

0

Your data option is wrong. It should be a Javascript object, not a JSON string.

data: {"ip": myip, "country": mycountry, "page": myurl},

Also, your server script doesn't return JSON, so you should not have:

dataType: 'json',
Barmar
  • 596,455
  • 48
  • 393
  • 495
  • Still not working, Sir. I saw the error like this at scribefire: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at myhost.com/guest-catcher/guest-post.php. This can be fixed by moving the resource to the same domain or enabling CORS. Is that is the problem? – toha Nov 21 '14 at 01:21
  • If the server doesn't allow cross-origin AJAX, there's nothing you can do in the client code. – Barmar Nov 21 '14 at 01:29
  • I have add header("Access-Control-Allow-Origin: OPTION "); at guest-post.php, but still not working.. How to make the server allow cross origin ajax? – toha Nov 21 '14 at 01:36
  • 1
    It should be `header("Access-Control-Allow-Origin: *")` – Barmar Nov 21 '14 at 01:40
  • I have tried it before, and failed too. What is the problem, Sir? – toha Nov 21 '14 at 01:45
  • I don't know, I don't have much experience with CORS. There are plenty of questions about it on SO, search for them. – Barmar Nov 21 '14 at 01:46
  • finally, my script is working http://pastebin.com/pBE7ApyX http://pastebin.com/n91xLWam – toha Nov 21 '14 at 02:10
0

You have to convert your data like this: data=JSON.stringify(your data);.

Example:

var d = {...}; # your dict.
$.ajax({                      
    type:'post',
    contentType:'application/json',
    url:type url here,
    dataType:'json',
    data:JSON.stringify(d),
    beforeSend:function(){

    },
    success:function(){

    }
});
yceruto
  • 8,307
  • 5
  • 33
  • 61
iammehrabalam
  • 985
  • 2
  • 11
  • 20
0

Please replace the ajax code with this and check.

$.ajax({
    type: 'POST',
    url: 'http://myhost.com/guest-catcher/guest-post.php',
    crossDomain: true,
    data: 'ip=' + myip + '&country=' + mycountry + '&page=' + myurl,
    success: function(responseData, textStatus, jqXHR) {
        alert("success");
    },
    error: function (xhr, status, error) {
        alert(xhr.responseText);
    }
});
joan16v
  • 4,551
  • 2
  • 44
  • 45
Lakhan
  • 9,570
  • 3
  • 17
  • 28
  • Still not working, Sir. I saw the error like this at scribefire: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at myhost.com/guest-catcher/guest-post.php. This can be fixed by moving the resource to the same domain or enabling CORS. Is that is the problem? – toha Nov 21 '14 at 01:21
-2
$.ajax({
    type: "POST",
    url: "some.php",
    data: { name: "John", location: "Boston" } // or  data: "name=" + value  + "location=" + value
})
Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
Gujian
  • 1
  • 1
  • This has been flagged as low-quality. Perhaps you should format your code and add some explanation about what your code does exactly, or your answer could be deleted by the community. – nalply Nov 20 '14 at 12:11