将值从ajax页面传递到调用页面javascript函数

时间:2022-12-07 23:59:39

One part of my page is being loaded via ajax using jquery. For example this initial page has a name first.php. It has a div with it's innerHTML generated from ajax called script (for example ajax is calling second.php). Is it possible to pass some values from ajax executed script (second.php) to original site. I need to access this value from original site (the one calling second script via ajax) javascript function, and I don't want to use hidden fields.

我的页面的一部分是使用jquery通过ajax加载的。例如,这个初始页面的名称为first.php。它有一个div,它的内部HTML由ajax生成,称为脚本(例如ajax调用second.php)。是否可以将一些值从ajax执行的脚本(second.php)传递到原始站点。我需要从原始站点(通过ajax调用第二个脚本的一个)javascript函数访问此值,我不想使用隐藏字段。

For example, my site has some captcha that is being displayed and processed through ajax. I don't want to write captcha result to some hidden field and access it with original site javascript function because of possible javascript injection attack...

例如,我的网站有一些通过ajax显示和处理的验证码。我不想将验证码结果写入一些隐藏字段并使用原始网站javascript函数访问它,因为可能的javascript注入攻击...

1 个解决方案

#1


0  

Since you call your secound.php script via ajax, you surely could read the result.

由于你通过ajax调用你的secound.php脚本,你肯定可以读取结果。

$.ajax({
  url: 'secound.php',
  success: function(data) {
    // now data contains the code returned by secound.php
  }
});

Now the most common way to return data from your secound.php script is returning it in JSon format. Then you could do someting like:

现在,从secound.php脚本返回数据的最常用方法是以JSon格式返回它。然后你可以做一些像:

var obj = jQuery.parseJSON(data);
alert(obj.name);

For this example your secound.php needs to return

对于此示例,您的secound.php需要返回

{"name":"John"}

#1


0  

Since you call your secound.php script via ajax, you surely could read the result.

由于你通过ajax调用你的secound.php脚本,你肯定可以读取结果。

$.ajax({
  url: 'secound.php',
  success: function(data) {
    // now data contains the code returned by secound.php
  }
});

Now the most common way to return data from your secound.php script is returning it in JSon format. Then you could do someting like:

现在,从secound.php脚本返回数据的最常用方法是以JSon格式返回它。然后你可以做一些像:

var obj = jQuery.parseJSON(data);
alert(obj.name);

For this example your secound.php needs to return

对于此示例,您的secound.php需要返回

{"name":"John"}