jQuery的JSONP呼叫朝PHP后台:没有HTTP响应 [英] jQuery JSONP Call towards PHP backend: No HTTP response

查看:279
本文介绍了jQuery的JSONP呼叫朝PHP后台:没有HTTP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新IV /当前状态:我现在已经证实低谷的另一个问题是,文件的字符编码​​是好的,而不是问题的原因。我还测试了针对另一台服务器的错误仍然存​​在。但它确实对本地主机的工作。 总结一下:的JSONP调用工作对本地主机,但反对外部域运行时,从服务器的响应是空的(无头/无HTTP响应code)。当复制请求的URL,并直接在浏览器中插入它时,输出是正确的正确格式化(UTF-8 / JSON)。

小提琴: http://jsfiddle.net/5SJvp/1/

更新三:我现在可以得到它的工作成功地在本地主机上。然而,使用完全相同的code(客户端和服务器)对我的生产领域仍然失败。来自服务器的响应是空意思就是说它没有返回HTTP状态code。

更新二:经过一些调试我注意到,响应不包含HTTP状态code。这可能是我的问题的原因是什么?我认为,这意味着有什么不对的服务器端,但我不能为我的生活中看到。

更新我:剪断从jQuery的哪里的请求似乎停止

  //不要发送请求
//这可能会引发异常这实际上是
//在jQuery.ajax(这里所以没有的try / catch)来处理
xhr.send((s.hasContent&安培;&安培; s.data)|| NULL);
 

PARAMS(从萤火虫)

  _ 1356655864905
回调jQuery18308375673194150332_1356655863817
p 0522
PL 12
ŝ假
秘密  ##############################
ürequest12341299
 

请求(从萤火虫)

 接受文/ javascript中,应用程序/ JavaScript的应用/ ECMAScript中,应用程序/ x-ECMAScript中,* / *; Q = 0.01
接受编码gzip压缩,紧缩
接受语言NB-没有,NB; Q = 0.9,禁忌; Q = 0.8,无; Q = 0.6,NN-无; Q = 0.5,NN; Q = 0.4,EN-US; Q = 0.3,EN Q = 0.1
连接保持
主持人本地主机:8888
Referer的HTTP://本地主机:8888 / popup.html
用户代理的Mozilla / 5.0(Macintosh电脑;英特尔的Mac OS X 10.8; RV:17.0)的Gecko / 20100101 Firefox的/ 17.0
的X请求-随着XMLHtt prequest
 

原题:

我挣扎与什么似乎是一个普遍的问题,但我还没有找到一个解决方案。我试图执行一个非常简单的JSONP调用使用jQuery。的问题是,或者a)什么也没有发生或b),从服务器的响应是空的。

我试了几种不同的方法,同时使用$就法和$ .getJSON方法。两者产生相同的错误的结果。使用下面没什么code发生:使用Chrome浏览器的调试器,我可以看到,它只是停止其执行halffway槽的方法。但是使用Wireshark的,我可以看到客户端进行三次握手的方式正是如此prePARs的发送数据时,它只是没有做到这一点。

如果我删除回调=?它不执行,但是响应的格式不正确(或至少,我认为是这样,因为我只能看到标有红色的线在Firebug的响应)。

  $。阿贾克斯({
     网址:http://mydomain.com/asd.php,//"http://localhost:8888/index.php
     数据类型:JSONP,
     键入:GET,
     数据:{P:P,U:U,S:符号,PL:PL的,秘密的:秘密},
     的contentType:应用/ JSON的;字符集= UTF-8,
     异步:假的,
     成功:功能(数据){
        的console.log(什么+ data.test);
     },
     错误:功能(数据){
         的console.log(由于某种原因失败);
     }
 });
 

服务器code($回调= $ _GET [回调]

 < PHP的头?(内容类型:应用程序/ JSON;字符集= UTF-8);
。
。
。
$数据=阵列
(
    消息=> $消息,
    状态=> $状态code,
    长度=> strlen的($消息)
);

回声$回调。 (.json_en code($的数据)。);
出口;
?>
 

下面是手动输入的输入服务器的响应。

 了funcName({消息:0b7(cb6Gv40,状态:OK,长度:12})
 

解决方案

这显然不会的工作,如果你没有设置您的SSL证书正确。

这正常工作时,我改造的https到http: http://jsfiddle.net/eysEe/

 变种U =测试;
VAR P = 1234;
VAR符号= FALSE;
VAR PL = 16;
VAR秘密=c68f39913f901f3ddf44c707357a7d70;

$阿贾克斯({
    网址:http://serve.pin2pass.com?index.php
    数据类型:JSONP,
    键入:GET,
    数据: {
        P:P,
        U:U,
        S:符号,
        PL:PL,
        秘诀:秘密
    },
    的contentType:应用/ JSON的;字符集= UTF-8,
    异步:假的,
    成功:功能(数据){
        $('#测试)文本(data.message)。
    },
    错误:功能(数据){
        $('#测试)文本(自卫队)。
    }
});
 

您可以告诉你,如果有不好的SSL安装时,https://serve.pin2pass.com?index.php导致风险的网页。也许你从来没有打算把它在HTTPS模式?

Update IV / Current status: I've now confirmed trough another question that the character encoding of the file is fine and not the cause of the problem. I've also tested against another server and the error still persists. It does however work towards localhost. So to summarize: JSONP call works towards localhost, but when running against external domains the response from the server is empty (no header / no http response code). When copying the requested URL and inserting it directly in a browser, the output is correct with correct formating (utf-8 / json).

Fiddle: http://jsfiddle.net/5SJvp/1/

Update III: I'm now able to get it working succesfully on localhost. However, using the exact same code (both client and server) towards my production domain it still fails. The response from the server is "empty" meaning to say it returns no http status code.

Update II: After some more debugging I noticed that the response does not include an http status code. This probably is the cause of my problem? I assume this means there is something wrong server side, but I cannot for the life of me see where.

Update I: Snip from jQuery where to request seems to halt.

// Do send the request
// This may raise an exception which is actually
// handled in jQuery.ajax (so no try/catch here)
xhr.send( ( s.hasContent && s.data ) || null ); 

Params (from Firebug)

_   1356655864905
callback    jQuery18308375673194150332_1356655863817
p   0522
pl  12
s   false
secret  ##############################
u   request12341299

Request (from Firebug)

Accept  text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language nb-no,nb;q=0.9,no-no;q=0.8,no;q=0.6,nn-no;q=0.5,nn;q=0.4,en-us;q=0.3,en;q=0.1
Connection  keep-alive
Host    localhost:8888
Referer http://localhost:8888/popup.html
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20100101 Firefox/17.0
X-Requested-With    XMLHttpRequest

Original question:

I'm struggling with what seems to be a common problem, but I've yet to find a solution. I'm trying to execute a very simple jsonp call using jQuery. The problem is that either a) nothing is happening or b), the response from the server is empty.

I've tried several different approaches, using both the $.ajax method and the $.getJSON method. Both produce the same faulty result. Using the code below nothing happens: Using the Chrome debugger I can see that it simply stops its execution halffway trough the method. However using Wireshark I can see that the client performs the three way handshake and thusly prepars to send data, it just fails to do that.

If I remove the callback=? it does execute, however the response is malformed (or at least, I think so since I can only see a response marked with a red line in Firebug).

$.ajax({
     url: "http://mydomain.com/asd.php", //"http://localhost:8888/index.php",
     dataType: 'jsonp',
     type: 'GET',
     data: {p:p, u:u, s:symbols, pl:pl, secret:secret},
     contentType: "application/json; charset=utf-8",
     async: false,
     success: function(data){
        console.log("What " + data.test); 
     }, 
     error: function(data){
         console.log("failed for some reason");
     }
 }); 

Server code ($callback = $_GET["callback"]

<?php header('content-type: application/json; charset=utf-8');
.
.
.
$data = array
(
    "message" => $message,
    "status" => $statuscode,
    "length" => strlen($message)        
);

echo $callback . '('.json_encode($data) .')';
exit;       
?>

Here is the server response with manually typed input.

funcName({"message":"!0b7(cb6Gv40","status":"OK","length":12})

解决方案

This will obviously NOT work if you did not set up your SSL certificates properly.

This works properly when I transform the https to http: http://jsfiddle.net/eysEe/

var u = "test";
var p = 1234;
var symbols = false;
var pl = 16;
var secret = "c68f39913f901f3ddf44c707357a7d70";

$.ajax({
    url: "http://serve.pin2pass.com?index.php",
    dataType: 'jsonp',
    type: 'GET',
    data: {
        p: p,
        u: u,
        s: symbols,
        pl: pl,
        secret: secret
    },
    contentType: "application/json; charset=utf-8",
    async: false,
    success: function(data) {
        $('#test').text(data.message);
    },
    error: function(data) {
        $('#test').text("SDf");
    }
});

You can tell if you have bad SSL installation when "https://serve.pin2pass.com?index.php" leads to a risky page. Maybe you never intended to put it in https mode ?

这篇关于jQuery的JSONP呼叫朝PHP后台:没有HTTP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆