dongxilan9351 2013-06-19 15:02
浏览 46
已采纳

Jquery AJAX - JSON或TEXT

I'm trying to retrieve the TEXT from the getsku javascript after submitting but not sure how to really do it. 1) How do i retrieve the POST data?

2) How do i retrieve and post it back , if i have multiple varaible to pass back (Datatype:text)

3) When should i use JSON, and when text.

4) If i'm using JSON how do i read it(after javascript) and display it(returned data to javascript).

javascript in main page

 function getsku(){

$.ajax({
    type: "POST",
    url: "funcAjax.php",
    data: { 'ddl1': $("#drop_1").val(), 'ddl2': $("#tier_two").val() },
    dataType: 'text', 
    success: function(data) {
        $("#sku").val(data);
    },
    complete: function() {
        alert('Complete: Do something.');
    },
    error: function() {
        alert('Error: Do something.');
        }
});

}

Button

<input type="button" value="Get SKU" onclick="getsku();" >

Trying to retrieve from another php and return a data to the above php(Having Issues here)

if(isset($_REQUEST['ddl1'])) { 
echo "FOUND1";
}else{
echo "FOUND2";
}
  • 写回答

1条回答 默认 最新

  • dongzhuoxie1244 2013-06-19 15:11
    关注

    Always return JSON from your PHP. Then you can include as many variables as you need in your response, and an error code as well if appropriate - like this:

    {"error":"0","result1":"result 1 data","result2":"result 2 data"}
    

    Then your success function can become:

    success: function(data) {
        if (data.error != 0) {
            // An error occurred on server: do something 
        } else {
             $("#sku").val(data.result1);
             // do something with data.result2
        }
    },
    

    Your PHP would become something like this:

    if(isset($_REQUEST['ddl1'])) { 
      echo json_encode(array("error"=>0, "result1"=>"FOUND1"));
    }else{
      json_encode(array("error"=>1, "result1"=>"NotFOUND"));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题