-1

The following script returns a failure, even though it used to work:

function postToPHP(data_to_send) {
    $.ajax({
        type: "POST",
        datatype: "json",
        async: false,
        url: "http://school.edu/myurl/write_to_mongo.php",
        data: data_to_send,
        success: function ($msg) {
            alert('success');
            return;
        },
        error: function () {
            alert('failure to send to database');
        }
    });

In case it matters, it interacts with the following PHP script:

//Get data from .ajax call
$data = $_POST;

//Open mongo and select database
$m = new Mongo();
$db = $m->selectDB("numbers");

//select a collection
$collection = $db->testData;

//insert data
$collection->insert($data);

//find collection contents
$cursor = $collection->find();

echo $data;
?>

Any idea what's gone wrong? My Safari debugger is showing me "failure to load resource. There was a bad response from server.", but hunting this down on Google isn't producing anything useful; I've already made sure I'm importing jQuery properly.

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
  • 1
    Why pass `$msg` if it's not inside your `success: function`? – StackSlave Dec 11 '13 at 01:51
  • 1
    For debugging purposes, swap async: false, to true and see if the error message is any better. Are you calling a different domain or sub domain? What does the network tab show? – epascarello Dec 11 '13 at 01:59
  • Changing async: false to true produces no message; neither a failure one nor a success one. – user2548848 Dec 11 '13 at 04:05

3 Answers3

2

Cannot find server at http://school.edu/myurl/write_to_mongo.php when put url into address bar. Plus they would have to allow CORS. Therefore, if they do allow CORS then the Server is down, otherwise it's a Same Origin Policy issue.

StackSlave
  • 10,613
  • 2
  • 18
  • 35
0

Read Same_origin_policy.

AJAX can only communicate within same domain url.


Check if page url is not found .

function postToPHP(data_to_send) {
    $.ajax({
        type: "POST",
        datatype: "jsonp", //for crossDomain request
        async: false,
        crossDomain: true, //crossDomain request
        url: "http://school.edu/myurl/write_to_mongo.php",
        data: data_to_send,
        success: function ($msg) {
            alert('success');
            return;
        },
        error: function () {
            alert('failure to send to database');
        },
        statusCode: {
            404: function () { // if page is not found
                alert("page not found");
            }
        }
    });
}


How to get a cross-origin resource sharing (CORS) post request working

What is JSONP all about?

Community
  • 1
  • 1
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
0

I ended up fixing the issue by having the url in the .ajax request be local, e.g.:

url: "write_to_mongo.php"

For whatever reason, giving a full url caused it to be treated as originating from a different domain even though it wasn't.