如果Json响应包括斜杠,则无法从Ajax调用获取响应

时间:2022-10-08 08:52:37

I'm developing jquery mobile app and trying to make ajax call on my localhost, when i try to get json value with ajax, it gives these errors jQuery.ajaxTransport.send jQuery.extend.ajax ..

我正在开发jquery移动应用程序并尝试在我的localhost上进行ajax调用,当我尝试用ajax获取json值时,它会给出这些错误jQuery.ajaxTransport.send jQuery.extend.ajax ..

I'm really confused about this situation and i couldn't find any solution. If i remove or replace the slashes before ajax gets the json value from my script, it works fine, but i have lots of url value in my json and dont want to replace all of them. I have to learn why is it happening. Hope you can help me.

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

Json Value

[{"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 side

$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 */

1 个解决方案

#1


1  

Look at this solution :

看看这个解决方案:

Ajax :

$.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;

?>

it's returning :

它正在回归:

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

Hope that helps.

希望有所帮助。

Edit :

if you want to use ajax cross-domain you can use jsonp in your ajax type.

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

#1


1  

Look at this solution :

看看这个解决方案:

Ajax :

$.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;

?>

it's returning :

它正在回归:

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

Hope that helps.

希望有所帮助。

Edit :

if you want to use ajax cross-domain you can use jsonp in your ajax type.

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