【问题标题】:Can't Get Response From Ajax Call If Json Response Includes Slash如果 Json 响应包含斜杠,则无法从 Ajax 调用中获得响应
【发布时间】:2015-03-02 00:01:17
【问题描述】:

我正在开发jquery mobile 应用程序并尝试在我的本地主机上调用ajax,当我尝试使用ajax 获取json 值时,它会给出这些错误jQuery.ajaxTransport.send jQuery.extend.ajax ..

我真的对这种情况感到困惑,我找不到任何解决方案。如果我在ajax 从我的脚本中获取json 值之前删除或替换斜杠,它工作正常,但我的json 中有很多url 值并且不想替换所有这些值。我必须了解为什么会这样。希望你能帮助我。

Json 值

[{"Video_URL":"http:\/\/localhost\/video-1-season-trailer\/","Subtitle_URL":"http:\/\/localhost\/wp-content\/uploads\/abc.srt","Video_Image":"http:\/\/localhost\/wp-content\/uploads\/small-icons\/aaa.jpg","post_title":"Video Season 1","post_id":13649,"post_view":"2359","engsub":"none"}]

Ajax

$.ajax({
    type: "POST",
    url: 'http://localhost/androidjsonvol/?kategorisezon='+get_Cat+'&sezon=true&callback=?',
    dataType: "json", // or jsonp
    success: function(cat_Response){
       var myJsonString = JSON.stringify(cat_Response);
       $.mobile.changePage('bolum.html', { data : myJsonString, reloadPage : true, changeHash : true });

        }, error:function (xhr, ajaxOptions, thrownError){
         //error log
        }
  });

php端

$series_infos[]= array(
 'Video_URL'=>$video_url, /* e.g http://localhost/video-1-season-trailer/ */
 'Subtitle_URL'=>$video_sub,
 'Video_Image'=>$small_icon,
 'post_title'=>$post_title,
 'post_id'=>$post_id,
 'post_view'=>$view_count,
 'engsub'=>$engsub 
);

//for json 
 echo json_encode($series_infos);
// for jsonp
//echo $_GET['callback']. '(' . json_encode($series_infos) . ');';

/* ajax cant get this encoded value because it includes slash
 but somehow if i change the array values with any string 
 without slash e.g : "stringvaluesforarray" it works fine */

【问题讨论】:

  • 实际的响应正文是什么?
  • 抱歉,您的意思是错误响应文本吗?
  • 如果您提到的“Json 值”是实际的响应正文,那么您的代码应该没有任何问题。

标签: jquery ajax json jquery-mobile


【解决方案1】:

看看这个解决方案:

阿贾克斯:

$.ajax({
    type: "GET", // Depending on your type
    url: "your_file.php", // Your php file
    dataType: 'json', // Use jsonp to call cross-domain
    success: function(response) {
        // Using for each
        $.each(response, function(index, element) {
            // Do something with every element
            $('body').append(element);
        });
    }
});

PHP:

<?php

    // For example you have array of Urls
    $array = array('http://www.google.com', 'http://www.twitter.com', 'http://www.twitter.com');

    // Then storing the Json Encode in a variable
    $json = json_encode($array, JSON_UNESCAPED_SLASHES);

    // Remove Backslashes from that variable
    $parse = preg_replace('/\\\"/',"\"", $json);

    // Return the Urls after removing backslashes
    echo $parse;

?>

它回来了:

http://www.google.com
http://www.youtube.com
http://www.twitter.com

希望对您有所帮助。

编辑:

如果你想使用 ajax 跨域,你可以在你的 ajax 类型中使用 jsonp

【讨论】:

  • 感谢您的回复,我尝试了您的代码,但它并没有完全解决我的问题。但最后我找到了解决方案。这是关于跨域的。我正在处理PHP Storm,我的jquery 移动项目本地网址是:http://localhost:63342/,ajax 目标是http://localhost/some.php
  • 使用 Ajax Cross-Domain 的唯一方法是在 ajax 中使用 JsonP dataType。很高兴您找到了解决方案。
猜你喜欢
  • 2016-09-12
  • 2014-05-02
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
  • 1970-01-01
  • 2018-07-06
相关资源
最近更新 更多