JavaScript JQuery JSON Calls To PHP WebService Always Runs “Error” Callback jquery json call,jquery json call web service,jquery json

I'm having this headache now, since I've been having this problem the whole day and, still, can't fix it. I've looked on Google and StackOverflow for hours, tried many methods (including changing from JSON to JSONP, checking headers on PHP, localhost tests), asked friends, etc., and I'm still stuck. Maybe it's but a detail, I don't know.

I'm working on an Android mobile app, and for it, I have a PHP webservice on a hosting (let's say, example.com) that's working OK since I tested with with a PHP WS JSON client. Problem is I'm calling this WS now from a JS file on my computer using jQuery, JSON and Ajax, and I get the following response from Google Chrome's debugger console:

  • readyState: 4
  • statusText: "OK"
  • responseText: (what I need, no errors)

But on the response from the server, I always receive the Error callback, never the Success. I read it's because the server couldn't parse JSON correctly, but I don't really know.

I leave you my code.

From CLIENT.JS:

$.ajax({
    type: "POST",
    crossDomain: true,
    contentType: "application/json utf-8",
    dataType: "json",
    url: "http://www.example.com/ws/webservice.php/" + methodName,
    data: JSON.stringify(window.parameterArray),

    success: function (response)
        {
            alert('Success!');
            window.resultVar = "Success! " + response;
            console.log(response);
        },
    error: function (response)
        {
            alert('Error');
            window.resultVar = "Error: " + response;
            console.log(response);
        }
});

From SERVER.PHP:

<?php
header('Access-Control-Allow-Origin: *');  //I have also tried the * wildcard and get the same response
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
header('Content-type: application/json; charset=utf-8');

require_once "mobilefuncts.php";

$methodName = str_replace($_SERVER["SCRIPT_NAME"]."/", "", $_SERVER["REQUEST_URI"]);

if (isset($methodName))
{
   $param = (array)json_decode($HTTP_RAW_POST_DATA);

   $access = new MobileAccess();  //From mobilefuncts.php
   $result = call_user_func_array(array($access,$methodName), $param);   //Calls the method

   echo json_encode($result);
}
?>

Does anyone knows what can be done? Maybe, as I said before, the problem is but a detail. I don't know really, I'm kinda new to this kind of things.

Thanks in forehand!

UPDATE:

I just realized the Chrome console tells me this:

GET http://localhost:81/.../cordova_plugins.json 404 (Not Found)

Could it be the cause of the problem?

UPDATE 2:

Look here, I have a clue. I added more parameters to the error function, and got myself this result:

(The change in error is from function(response) to function(jqXHR, textStatus, errorThrown))

jqXHR.responseText: [an array with the info I'm asking]
errorThrown: "SyntaxError: Unexpected token"
Answer:1

because you are doing cross domain calls you need to use JSONP. The PHP server must form a response that looks like this: callback( jsonSyntax);

Answer:2

Change

contentType: "application/json utf-8",

to

contentType: "application/json; charset=utf-8",

The malformation of your contentType header is causing your server to incorrectly interpret the content of your POST request.

Answer:3

Try to change the JQuery URL from :

http://www.example.com/ws/webservice.php/methodName

To:

http://www.example.com/ws/webservice.php?method=methodName

And then try to read it from the $_GET['method'] from PHP, I think this should be your problem

Answer:4

I have a div in which I have used submit button as follow : <div id="collapse2" class="panel-collapse collapse"> <div class="panel-body"> <div class="logininput"> ...

I have a div in which I have used submit button as follow : <div id="collapse2" class="panel-collapse collapse"> <div class="panel-body"> <div class="logininput"> ...

  1. reload jquery datatable
  2. reload jquery after ajax call
  3. reload jquery datatable after ajax call
  4. reload jquery function
  5. reload jquery ajax
  6. reload jquery script
  7. reload jquery datatable with new data
  8. reload jquery datatable on button click
  9. reload jquery dialog
  10. reload jquery datatable with new parameters
  11. reload jquery function after ajax
  12. reload jquery script after ajax
  13. reload jquery div
  14. reload jquery validate
  15. reload jquery element
  16. reload jquery plugin
  17. reload jquery document.ready
  18. reload jquery table
  19. reload jquery event
  20. reload jquery on click

When resizing an element in isotope using perfectMasonry extention because of the variable sizes of the elements it creates some spaces. I want to check with jquery every time there is a relayout if ...

When resizing an element in isotope using perfectMasonry extention because of the variable sizes of the elements it creates some spaces. I want to check with jquery every time there is a relayout if ...

Assume that I have two literals: false and 1. If I invoke function toString() on both of them, I get: false.toString() // false 1.toString() // Uncaught SyntaxError: Unexpected token ILLEGAL ...

Assume that I have two literals: false and 1. If I invoke function toString() on both of them, I get: false.toString() // false 1.toString() // Uncaught SyntaxError: Unexpected token ILLEGAL ...

Consider the following code that contains a simplified implementation of Bluebird's Promise.settle: var a = Promise.reject('a'); var b = Promise.resolve('b'); var c = Promise.resolve('c'); var ...

Consider the following code that contains a simplified implementation of Bluebird's Promise.settle: var a = Promise.reject('a'); var b = Promise.resolve('b'); var c = Promise.resolve('c'); var ...