Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

javascript - JQuery JSON Calls To PHP WebService Always Runs "Error" Callback

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"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...