weixin_33690963 2014-12-16 08:49 采纳率: 0%
浏览 12

Ajax获取语法

I 've read few tutorials about ajax http request, but somehow i couldn't understand it enough to build a syntax i need.

var path = $(this).data('path');
    $.get('http://example.ro/index.php?page=contract.php&path='+path, function(){

    });

What i try to achieve is to change url (http://example.ro/index.php?page=contract.php) , without reloading the page,so i can use the path value for later use in a query or some other action,in the same page.But the url doesnt change (it must be with &path=value at the end),so i can't catch the value with something like:

if(isset($_GET['id'])){
/do something
}

Is the syntax wrong? or i cant do it in this way? Help please!

UPDATE

$(document).on('click','tr.listContractRow', function(e){

   var path = $(this).data('path');
    $.ajax({
  type: 'get',
    url: 'contract.php',
    data: {id: path},
    success: function(result) {

      console.log(result)
    },
    error: function() {

    }
});


});

The variable path is a value taken from a php content,and i want to take it with ajax and return it back so i can catch it with php.

  • 写回答

1条回答 默认 最新

  • 普通网友 2014-12-16 09:12
    关注

    Right syntax for an ajax get request made with jquery:

    $.ajax({
          type: 'get',
            url: 'yourphpscript.php',
            data: {
              key1: value1,
              keyn: valuen
            },
            success: function(result) {
              // to be executed on status 200 OK
              console.log(result) // to check what you sent back from your page
            },
            error: function() {
              //to be executed on status 500 error
            }
        });
    

    this is a pretty basic syntax.

    Let's assume you are on your page. And you want to call an ajax request, get something from it, store in your page, and send it back later to another php. with a submit or with another ajax.

    So step1:

    $.ajax({
          type: 'get',
            url: 'givemepath.php',
            data: {
              key1: value1,
              keyn: valuen
            },
            success: function(result) {
              // pretty ugly, but functional.
              window.path = result.path;
              // or if you plan to submit a form prepare an hidden field with id myhiddenfield
              $.('#myhiddenfield').val(result.path);
            },
            error: function() {
              //to be executed on status 500 error
            }
        });
    

    your givemepath.php

    <?php
      $path = ...some php code....
      header("content-Type: application/json");
      echo json_encode(array("path" => $path));
    ?>
    

    Later when you want to use again this value in your page

    $.ajax({
          type: 'get',
            url: 'sendpathbacklater.php',
            data: {
              path: window.path,
              otherdata: otherdata
            },
            success: function(result) {
              //sent it!
            },
            error: function() {
              //to be executed on status 500 error
            }
        });
    

    or just submit your form.

    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)