javascript 从 ajax 响应中获取特定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32549836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:27:42  来源:igfitidea点击:

Getting a particular value from ajax response

javascriptphpjqueryajaxjquery-masonry

提问by shubham sharma

I am using ajax with masonry

我在砖石中使用ajax

Ajax code: This ajax is used to get data from

Ajax 代码:这个ajax 用于从

$.ajax({
      type: "get",
      url: "some.php",
      dataType: "text",                 
      success: function(data) { 
        if (data && data.length > 0) {       
          $items = $(data); 
          $grid.append( $items )
          .masonry('appended', $items);
          $(this).find(".loading").hide();
        }
      }

Php Part: This is just a small or sufficient part of php file to understand the problem

php部分:这只是php文件的一小部分或足够了解问题的部分

$b= "sv";
echo "asjdgsajd";
echo "a";
echo $b;

now i am getting everything correctly but i want to use say value of $b for setting a attribute value and also other values as content but how can i particularly get value of $b? Thankyou

现在我正确地获取了所有内容,但我想使用 $b 的值来设置属性值以及其他值作为内容,但是我如何特别获得 $b 的值?谢谢

回答by some-non-descript-user

Change the dataTypeto json.

将 更改dataTypejson

$.ajax({
      type: "get",
      url: "some.php",
      dataType: "json",                 
      success: function(data) { 
        //data will hold an object with your response data, no need to parse
        console.log('Do whatever you want with ' + data.b + '.');
      }

In some.phpdo the following:

some.php执行以下操作:

$response =array(
    'b' => "sv",
    'a' => "asjdgsajd",
    'c' => "a"
);
echo json_encode($response);
echo $b;

The items of the associative array will end up as properties of a javascript object, that you can use in your successcallback (or donefunction as successis deprecated).

关联数组的项最终将作为 javascript 对象的属性,您可以在success回调中使用它(或不推荐使用的done函数success)。

回答by Amit.S

Try using json and change your php to send the json response that way you can send more content and access them on client side as you need it.

尝试使用 json 并更改您的 php 以发送 json 响应,这样您就可以发送更多内容并根据需要在客户端访问它们。

PHP Script :

PHP脚本:

$outArr=array("b"=>"sv","content1"=>"asjdgsajd","content2"=>"a");
$jsonResponse=json_encode($outArr);
echo $jsonResponse;

In AJAX function you can access your data like this:

在 AJAX 函数中,您可以像这样访问数据:

$.ajax({
  type: "get",
  url: "some.php",
  dataType: "text",                 
  success: function(data) { 
    if (data && data.length > 0) {       
      data=$.parseJSON( data ); //parse response string
      b=data.b;//value of b
      content1=data.content1;//value of content1
      $("#exampleDiv").attr("id",b).html(content1); //set the attribute and content here for an example div
    }
  }
})