0

$.ajax get data from server, the status is 200, but the response is empty. I using the same method to send my ajax request, some is normal, but there is some requests

/customer/api/show/r:571?nav=push

/customer/api/show/r:571?nav=push&status=sended

they don't have any response from server. The console always say: 'This request has no response data available'. But the requests like these are fine:

/customer/api/show/r:311?nav=report

/customer/api/show/r:30?nav=dashboard

... ...

Why?

Here is the details :

request ----------------------

var request = $.ajax({
    url: 'customer/api/show/r:' + Math.round(Math.random() * 1000),
    data: params,
    dataType: 'json',
    success: function (res) {
        /* do something, like: update the dom with the res.html */
    },
    error: function() {
    }
});

response ----------------------

General:
Request URL:http://test.local/api/show/r:31?nav=push
Request Method:GET
Status Code:200 OK
Remote Address:127.0.0.1:80

Response Headers:
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=utf-8
Date:Tue, 17 Jan 2017 08:58:50 GMT
Server:nginx/1.10.0
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Powered-By:PHP/5.5.36

......

the details (wireshark) about the request

Here is my server-side codes (PHP):

......
ob_start();
$this->render("customer/static/" . $nav . ".php"); /* html with php */
$result->html = ob_get_clean();

echo json_encode($result);

......

The "$this->render" methods:

public function render($filename, $output_content=TRUE) {
    global $C, $D;
    $filename = $C->INCPATH . '../html/' . $filename;
    if ($output_content) {
        require($filename);
        return TRUE;
    } else {
        ob_start();
        require($filename);
        $cnt = ob_get_contents();
        ob_end_clean();
        return $cnt;
    }
}

PS:

if ($nav !== 'push') {
    ob_start();
    $this->render('customer/static/' . $nav . '.php');
    $result->html = ob_get_clean();
} else {
    $result->html = 'testing';
}

The response is normal...

The Screen Shot about this.

Community
  • 1
  • 1
iTaoLR
  • 1
  • 3
  • Could be wrong but the "non-empty" packet in wireshark is 1 byte shorter than the empty one - are you certain the server is sending a response body? Maybe check the XHR's response in Chromes network tab to see what's there and double check that the server is actually sending a response body (maybe echo what you're sending to console or log it to a file just to be sure) – Brian Jan 18 '17 at 02:50
  • It may also be more beneficial to help troubleshoot the issue if you post the relevant server-side code relating to the problematic responses as I'm pretty sure that's where the issue will be :) – Brian Jan 18 '17 at 02:53
  • I appended the `server-side` codes right now. @Brian – iTaoLR Jan 18 '17 at 03:12
  • Why are you using output buffering? You seem to be calling `ob_start()` multiple times – Phil Jan 18 '17 at 03:18
  • The output buffering is a html file I use them as a template. @Phil – iTaoLR Jan 18 '17 at 03:26
  • I know I need to refactor this old project, even rewrite them all. They are disgusting. But firstly, I need to know the reason `why it doesn't work well`. That's the point for me now. Is there something wrong when using output buffering? @Phil Why is there some normal requests having response? – iTaoLR Jan 18 '17 at 03:39
  • Nothing wrong with using output buffering as needed it's even considered good practice in some scenarios. It can at times make debugging more painful however (unexpected exits won't show truncated output as buffer is never output etc.). Have you tested the problem templates on their own to ensure they output? I can't see anything glaringly wrong in the examples that you added (a little room for clean up but nothing glaringly broken) – Brian Jan 18 '17 at 03:56
  • Sorry I didn't spot this earlier - have you tried sending a content-type header appropriate for JSON data `header('Content-Type: application/json');` – Brian Jan 18 '17 at 04:07
  • The header is right... it's still puzzling >.< @Brian – iTaoLR Jan 18 '17 at 05:00
  • I think I've got it. Your requests that work/don't work have different port numbers - this causes them to be treated as cross-domain requests which will in turn cause jQuery to default to jsonp (even for `dataType:"json"`) unless you provide the option `jsonp: false` , setting jsonp to false may in turn require that appropriate CORS headers are sent with the response. http://api.jquery.com/jquery.ajax/ under `dataType` has a little more info – Brian Jan 18 '17 at 05:49
  • No, all my requests are the same domain and port... @Brian – iTaoLR Jan 18 '17 at 06:35
  • My bad - sorry I'm a bit tired today. Only other thought I've got is maybe have a look at the jqXHR object and see if anything is happening there - you could change the callback to something like `function(res, status, jqXHR){ console.log('responseText:', jqXHR.responseText)};` which should log out the un-parsed response and maybe shed some light on what's going on. – Brian Jan 18 '17 at 06:56
  • Thanks so much @Brian, I got it... Because the encoding of output of the file is not `UTF-8`, so the json_encode returns `null`... echo json_encode($result); echo json_last_error(); // returns 5 [link](http://stackoverflow.com/questions/1972006/json-encode-is-returning-null) – iTaoLR Jan 18 '17 at 08:31
  • I solved it temporarily with `base64_encoding` the content, and decoded them in the client [base64_ref](https://scotch.io/tutorials/how-to-encode-and-decode-strings-with-base64-in-javascript)... Would you tell me other better solutions? @Brian – iTaoLR Jan 18 '17 at 08:37
  • @iTaoLR no worries apparently `json_encode` will only work with UTF-8 strings. If it's possible to convert the output to UTF-8 using something like `mb_convert_encoding` that would be the ideal scenario I think, failing that you might need to create a `json_encode` function for whatever encoding you're using and also provide the appropriate headers on the `content-type` header ie. `header("Content-type: application/json; charset=UTF-16");` the PHP docs for `json_encode` have some good starting points in the comments http://php.net/manual/en/function.json-encode.php I hope this is helpful – Brian Jan 18 '17 at 08:55
  • Got it, thanks @Brian – iTaoLR Jan 19 '17 at 03:51
  • @iTaoLR you're very welcome - just out of interest which solution did you end up settling on? May be worth answering your own question if it may benefit others with a similar in future :) – Brian Jan 19 '17 at 04:07
  • 1
    I use `base64_encode` to encode the string that user inputs... I just solved it temporarily and didn't try `mb_convert_encoding` and didn't try to change the character settings of the database's table structure (my colleagues created this table) ...... The `UTF-8` for the `json_encode` is the point I think ^.^ @Brian Thanks for your reminding. I'm so happy others can benefit from this question in the future. – iTaoLR Jan 19 '17 at 04:49

0 Answers0