1

I am using WAMP 3.0.8 PHP 7.0.10 Apache 2.4.23 and trying to build my first JSON API.

The ajax call does send the data to the server. I can see it stored in the text file.

My AJAX looks like this.

$(document).ready(function(){
    $('#order').click(function(){

         var send = {"name":"Jo Moe", "age":39, "city":"Meridian"};
         var prescription = JSON.stringify(send);
         $.ajax({
            type: 'POST',
            dataType: 'JSON',
            url: 'https://apa.superserver.com/receive.php',
            data: {"scripts": prescription},
            success: function(data){
                console.log(data);
                alert(data);
                $('#success').html('<p>Finished</p>');
            }           
        });

    });

});

Server side looks like this:

if(isset($_POST["scripts"]) && !empty($_POST["scripts"])){
    $sent = filter_input(INPUT_POST, "scripts", FILTER_SANITIZE_SPECIAL_CHARS);
    $scripts = html_entity_decode($sent);

    file_put_contents("jsonScript.txt", $scripts);

    $finish = "Message Received!";

    echo json_encode($finish);

}

Now this all worked when it was on the same server. When I separated the receive.php and put it on the remote server. It no longer shows the response.

Thanks!

UPDATE:

Get json ajax php form to work remotely

The link above fixed my issue and gave me more information in the console to figure out what was happening. (I should not have been given a -1) The problem with searching the index here that if you don't use the exact terms in the post it is nearly impossible to find. (my two cents)

Adding the error function and changing from "data" to "response" made the difference along with the xhr. I can see everything.

This my return to the console:

Object {readyState: 4, responseText: "↵↵hello!"Message Received!"", status: 200, statusText: "OK"}
  ajax.php:56 parsererror
  ajax.php:57 SyntaxError: Unexpected token h in JSON at position 0
  at JSON.parse (<anonymous>)
  at parseJSON (jquery-1.6.4.min.js:2)
  at b$ (jquery-1.6.4.min.js:2)
  at w (jquery-1.6.4.min.js:4)
  at XMLHttpRequest.d (jquery-1.6.4.min.js:4)

As you can see the response text is what the server is sending back. I did have to modify my httpd.conf to turn on the mod_headers.c and added an .htaccess .

UPDATE 2:

Figured out what was causing the parse error. I had a print "hello!" line in my server side file that I had there for other troubleshooting. Once I removed it and had only the posted server side code. Everything is peachy now. Onward!!

Community
  • 1
  • 1
user1794918
  • 1,131
  • 2
  • 16
  • 34
  • What does `receive.php` returns ? File contents (raw PHP) ? – zronn Feb 14 '17 at 22:07
  • Pop up the console in your browser and check the response from the server. It will probably answer something like you're trying to post things Cross Origin, which isn't allowed by default... [Take a look here](https://enable-cors.org/server_php.html) on how you _could_ resolve this... – Raphioly-San Feb 14 '17 at 22:07
  • There is nothing in the console. I used the console to fix the NO ACCESS-CONTROL-ORIGIN error and after that I have not gotten any more errors. – user1794918 Feb 14 '17 at 23:43

1 Answers1

-1

If I understand correctly You want to make ajax request to another domain. It's blocked because of Same origin policy. To achieve your goal You can implement JSONP

Cichy
  • 109
  • 5