关于ajax问题,在线等,先谢谢各位了
index.php页面有两个div,ID分别为div1,div2,对应的事件为:
$.ajax({ url: test.php, type: 'post', dataType:'text', success: function (responseText) { $('#div1').html(responseText); $('#div2').html(responseText); } });
有没有办法将test.php页面返回的值分别放到div1,div2呢?我想到的是将返回的值用js来切隔,然后放到里面,还没有实践,不知行不行,如果可以,我觉得这样好像不是很好。请问有其他的方法吗?
回复讨论(解决方案)
完全可以,只是你要注意
responseText
里面,不能包含你需要切割的那个字符。
推荐你用json,这样比较方便些。就不会涉及切割了。
可以用 js 切割,但你得有唯一的切割标志吧?
不然把正文也切割了,就不美了
返回多个数据一般用 json
$res = array( 'div1' => '相关内容', 'div2' => '相关内容',)echo json_encode($res);
$.ajax({ url: 'test.php', type: 'post', dataType:'json', success: function (data) { $('#div1').html(data.div1); $('#div2').html(data.div2); }});
更一般的
$.post('test.php', {}, function(d) { for(var i in d) $('#'+i).html(d[i]);}, 'json');
完全可以,只是你要注意
responseText
里面,不能包含你需要切割的那个字符。
推荐你用json,这样比较方便些。就不会涉及切割了。
为什么还“不能包含你需要切割的那个字符。”?比如说我得到的“中华人民共和国{|}国和共民人华中”,要有{|}这个才好切啊。
完全可以,只是你要注意
responseText
里面,不能包含你需要切割的那个字符。
推荐你用json,这样比较方便些。就不会涉及切割了。
好的,我先试试哈
完全可以,只是你要注意
responseText
里面,不能包含你需要切割的那个字符。
推荐你用json,这样比较方便些。就不会涉及切割了。
刚刚回的应该是回复你,哈哈,成功完成,谢谢。
完全可以,只是你要注意
responseText
里面,不能包含你需要切割的那个字符。
推荐你用json,这样比较方便些。就不会涉及切割了。
为什么还“不能包含你需要切割的那个字符。”?比如说我得到的“中华人民共和国{|}国和共民人华中”,要有{|}这个才好切啊。
假设你要按"|"切割,这样你看对不对:
//显然是对的
这是测试数据|这是测试数据
//这样就不对了
这是|测试|数据|这是|测试|数据
所以你还是用json吧,方便
用json,根?key?取value,??就方便了。
基本同意楼上观点,用json 键值对可以处理很多问题比text好用
可以用 js 切割,但你得有唯一的切割标志吧?
不然把正文也切割了,就不美了
返回多个数据一般用 json
$res = array( 'div1' => '相关内容', 'div2' => '相关内容',)echo json_encode($res);
$.ajax({ url: 'test.php', type: 'post', dataType:'json', success: function (data) { $('#div1').html(data.div1); $('#div2').html(data.div2); }});
更一般的
$.post('test.php', {}, function(d) { for(var i in d) $('#'+i).html(d[i]);}, 'json');
奇怪了,我回复了两次,是引用你的,都变成第一个回复的了。
可以用 js 切割,但你得有唯一的切割标志吧?
不然把正文也切割了,就不美了
返回多个数据一般用 json
$res = array( 'div1' => '相关内容', 'div2' => '相关内容',)echo json_encode($res);
$.ajax({ url: 'test.php', type: 'post', dataType:'json', success: function (data) { $('#div1').html(data.div1); $('#div2').html(data.div2); }});
更一般的
$.post('test.php', {}, function(d) { for(var i in d) $('#'+i).html(d[i]);}, 'json');
奇怪了,我回复了两次,是引用你的,都变成第一个回复的了。完美解决,谢谢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

How to use JavaWebsocket to implement online audio and video calls? In today's digital age, real-time communication is becoming more and more common. Whether it is remote collaboration at work or remote communication with relatives and friends at home, real-time audio and video calls have become an indispensable part of people. This article will introduce how to use JavaWebsocket to implement online audio and video calls, and provide specific code examples. 1. Understand WebsocketWebsocket is a new technology in HTML5

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.

In order to improve Ajax security, there are several methods: CSRF protection: generate a token and send it to the client, add it to the server side in the request for verification. XSS protection: Use htmlspecialchars() to filter input to prevent malicious script injection. Content-Security-Policy header: Restrict the loading of malicious resources and specify the sources from which scripts and style sheets are allowed to be loaded. Validate server-side input: Validate input received from Ajax requests to prevent attackers from exploiting input vulnerabilities. Use secure Ajax libraries: Take advantage of automatic CSRF protection modules provided by libraries such as jQuery.
