1

EDIT: Duplicate

Phonegap Cordova Ajax requests 404 (Not Found) Error

I am creating an android phonegap application and I have come across a problem which has never caused any problems in the past. The only thing I can think of is that I changed hosting recently.

The code below returns a 404 status when trying to POST data to a PHP file.

Sample data variable:

 {q: 8, Email: "example@gmail.com", Pin: "0000"}

Ajax Call

function CallAjax(data, successFn, errorFn) {

$.ajax({

    url: "http://www.gigtimes.co.uk/app/test.php",
    data: {postdata: data},
    async: true,
    type: 'POST',
    dataType: 'json',
    success: successFn,
    error: errorFn
  });

    return false;
}

When I access http://www.gigtimes.co.uk/app/test.php through the browser it works perfectly fine, but when accessing it through the app it returns a 404.

test.php

<?php

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");

echo "{\"message\": \"hello\"}";

?>

Any help is appreciated. Thanks

Community
  • 1
  • 1
SkelDave
  • 1,176
  • 2
  • 9
  • 23
  • `success: function(res){console.log(res);}` instead of `success: successFn,` – Alive to die - Anant Mar 29 '16 at 16:30
  • You may also have trouble with JSON parsing because your JSON is not valid. It is currently using single quotes, but the spec requires double quotes. http://www.json.org – Jonathan M Mar 29 '16 at 16:35
  • I have changed the success callback and logged the result and it still shows 404 not found in chrome dev tools. I have also changed it to be valid JSON and it still doesn't work. Thanks anyway. – SkelDave Mar 29 '16 at 16:52
  • Is the page that is making the ajax call served from gigtimes.com also? – Jonathan M Mar 29 '16 at 16:55
  • The ajax call is coming from a phonegap application and it is requesting the PHP file from gigtimes.co.uk, not gigtimes.com – SkelDave Mar 29 '16 at 16:59
  • I'm wondering if the location in the request has been white-listed in phonegap: http://docs.build.phonegap.com/en_US/configuring_access_elements.md.html – Jonathan M Mar 29 '16 at 17:08
  • 1
    wow great! I read this post just before your answer http://stackoverflow.com/questions/30048453/phonegap-cordova-ajax-requests-404-not-found-error. Thanks for the help, the problem is solved. – SkelDave Mar 29 '16 at 17:12

0 Answers0