dongyanhu5628 2012-09-25 20:54
浏览 50

简单的jQuery - php通信

i'm working on an php-application, and now i have to build an interface to communicate between jquery and php. i already have a working controller/action. what is the best way to communicate with jQuery, if i need to send an array to php in the request.

example: data that is needed to fulfill the request:

$request_data = array 
(
  'key1' => 'value1',
  'list1' => array
  (
    'listkey1' => 'listvalue1',

  )
)

should i send this in different as "normal" post-request, or is it easier to send one post-var with the array encoded in json? ($post_json="{key: value....})

the focus is on easy integration in jquery

edit: it is not about passing the data to the client, it is about passing the array from the client to the php-script

  • 写回答

2条回答 默认 最新

  • dsla94915 2012-09-25 21:01
    关注

    use phps json_encode() to generate the response, and read it with jquery on client?

    $.getJSON('youpage.php', function(data) {
      $.each(data, function(key, value) {
        alert(key + '=>' + value);
      });
    }
    

    http://php.net/manual/en/function.json-encode.php

    UPDATE

    in regards to communication the other way, from the client to php. It all depends on the task at hand really. If the data in the drop down purely depend on the entries on the form, and they are otherwise stateless. Then,the best route will depends on your backend code, you could do a ajax post, with the individual variables, or concatenate them into one variable before sending, and then splitting them up on the backend. Or, as you say, you could create a json string and then use json_decode on the backend.

    Which route is fit for purpose depends on many factors, and I dont think there is a right or wrong error.

    Personally, I would generate a AJAX post request (im not a php coder though) to the backend, and then process the request object directly. You still would need to process a data structure, so why add the overhead/extra-step of deserializing json from the request.

    评论

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案