最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Ajax return multiple data - Stack Overflow

programmeradmin0浏览0评论

I am using jQuery AJAX to return a string from PHP which consists of some JavaScript, PHP and HTML.

I can successfully do this with the following code:

header("Content-Type: text/html");
echo $content;

$.ajax({
    type: 'POST',
    url: url,
    data: data,
}).done(function(result) {

}).fail(function(jqXHR, textStatus, errorThrown) {
    console.log(jqXHR, textStatus, errorThrown);
});

The problem I have now is that I want to return some other simple values along this string as well.

But if I use json_encode to send an array of these values, it will break my string and won't be successful.

How could I send one value as string (without json_encode) and some other values with json_encode? (so I don't json_encode my string)

EDIT1:

Here is formatting issue 1:

return 'autoOpenPopup: '.!empty($options["autoOpenPopup"]) ? $this->int_to_bool($options["autoOpenPopup"]) : $this->int_to_bool(false) . PHP_EOL .';

2:

return '.!isset($options["popupInit"]) ?

                $playerId.' = jQuery("#'.$wrapperId.'").hap(settings);

            ':'

                if(hasLocalStorage){

                    if(!localStorage.getItem("hap_popup_fixed")){
                        '.$playerId.' = jQuery("#'.$wrapperId.'").hap(settings);
                    }
                }else{
                    '.$playerId.' = jQuery("#'.$wrapperId.'").hap(settings);  
                }

I am using jQuery AJAX to return a string from PHP which consists of some JavaScript, PHP and HTML.

I can successfully do this with the following code:

header("Content-Type: text/html");
echo $content;

$.ajax({
    type: 'POST',
    url: url,
    data: data,
}).done(function(result) {

}).fail(function(jqXHR, textStatus, errorThrown) {
    console.log(jqXHR, textStatus, errorThrown);
});

The problem I have now is that I want to return some other simple values along this string as well.

But if I use json_encode to send an array of these values, it will break my string and won't be successful.

How could I send one value as string (without json_encode) and some other values with json_encode? (so I don't json_encode my string)

EDIT1:

Here is formatting issue 1:

return 'autoOpenPopup: '.!empty($options["autoOpenPopup"]) ? $this->int_to_bool($options["autoOpenPopup"]) : $this->int_to_bool(false) . PHP_EOL .';

2:

return '.!isset($options["popupInit"]) ?

                $playerId.' = jQuery("#'.$wrapperId.'").hap(settings);

            ':'

                if(hasLocalStorage){

                    if(!localStorage.getItem("hap_popup_fixed")){
                        '.$playerId.' = jQuery("#'.$wrapperId.'").hap(settings);
                    }
                }else{
                    '.$playerId.' = jQuery("#'.$wrapperId.'").hap(settings);  
                }
Share Improve this question edited Jul 14, 2016 at 22:37 Toniq asked Jul 14, 2016 at 17:50 ToniqToniq 5,02814 gold badges63 silver badges126 bronze badges 4
  • What about put your string into the array then json_encode both? – Chay22 Commented Jul 14, 2016 at 17:54
  • I tried json_encode just on my string and it breaks. Putting it in array doesnt seem to change anything: pastie/10907465 – Toniq Commented Jul 14, 2016 at 18:01
  • Well, you should post the actual code to find what makes it break. – Chay22 Commented Jul 14, 2016 at 19:34
  • I edited my post with some data. – Toniq Commented Jul 15, 2016 at 9:35
Add a ment  | 

2 Answers 2

Reset to default 4

The best way is to json_encode your data and string together;

$data = array('some', 'array', 'elements');

$string = 'my string';

$data2 = array('more', 'data');

Then you bine all of them in one array:

$result  = array();
$result['data1'] = $data;
$result['string'] = $string;
$result['data2'] = $data2;

Finally json_encode the array:

echo json_encode($result); 

Then you read the result in JS:

$.ajax({
    type: 'POST',
    url: url,
    data: data,
}).done(function(result) {
  var jsonResult = $.parseJSON
  var data1 = result.data;
  var data2 = jsonResult.data2;
  var str = jsonResult.string;

}).fail(function(jqXHR, textStatus, errorThrown) {
    console.log(jqXHR, textStatus, errorThrown);
});
header('Content-type: application/json');
echo json_encode($data, true);

and in your $.ajax({...dataType:'json'});

发布评论

评论列表(0)

  1. 暂无评论