本文介绍了JQuery JSON调用PHP WebService始终运行“错误”打回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在头疼,因为我整天都遇到这个问题,但仍无法解决。我已经看了谷歌和StackOverflow几个小时,尝试了很多方法(包括从JSON更改为JSONP,检查PHP上的标题,localhost测试),问朋友等等,我仍然卡住了。也许这只是一个细节,我不知道。

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.

我正在开发Android移动应用程序,为此,我在主机上有一个PHP web服务(让我们说,example.com),因为我使用PHP WS JSON客户端进行测试,所以工作正常。问题是我现在使用jQuery,JSON和Ajax从我的计算机上的JS文件中调用此WS,我从Google Chrome的调试器控制台得到以下响应:

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 :(我需要什么,没有错误)

但是根据服务器的响应,我总是收到错误回调,而不是成功。我读它是因为服务器无法正确解析JSON,但我真的不知道。

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.

我留下了我的代码。

来自CLIENT.JS:

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);
        }
});

来自SERVER.PHP:

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.

感谢正手!

更新:

我刚刚意识到Chrome控制台告诉我:

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?

更新2:

看这里,我有一个线索。我在错误函数中添加了更多参数,并得到了自己的结果:

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

(错误的变化是从函数(响应) function(jqXHR,textStatus,errorThrown)

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


推荐答案

因为你正在进行跨域调用,所以你需要使用JSONP。 PHP服务器必须形成如下响应:callback(jsonSyntax);

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);

这篇关于JQuery JSON调用PHP WebService始终运行“错误”打回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 11:03