Hi,
I have used AJAX lots before and I wondered if it was possible to set variables from an AJAX file E.G. an ajax file could set
var set1 = 10;
var set2 = 55;
with both variables coming from an external file called by AJAX
Could you tell me the JS side for performing the request and the format of the file that is requested.
Regards,
Sam RUdge

Recommended Answers

All 4 Replies

The AJAX response just has to send like a JSON response then you parse the response and set them in your success callback.

JSON is certainly the way to go for complex data but for simple, predicatable stuff you can D.I.Y. with something like the solution I posted here:

http://www.daniweb.com/forums/post924561.html#post924561

Server-side there won't be much difference in speed/load but client-side will be (signifcantly?) less loaded by using a D.I.Y. approach.

Having said that, if any one AJAX call in an application requires JSON, you might as well use it for all, as the JSON lib will (for the vast majority of users) cache client-side.

Airshow

commented: Good response and link to relevent post +2

Thanx airshow, i guess your solution is kind ov like explode() in PHP but it works perfectly

Exactly so Samarudge.

Javascript's split()/join() do exactly the same thing as PHP's explode()/implode().

There's a difference in syntax in that js's split()/join() are methods of String and Array respectively, whereas in PHP explode()/implode() are top-level functions that take string/array as parameters.

Personally I prefer the js approach because it's easier to form a ".daisychain" of methods than to embed functions ever deeper in each other's parentheses. For example, the following js statement would be (imho) rather messy in php and arguably better coded on more than one line:
var str = .join(' ').replace('Hope', 'Trust').toUpperCase();

This is of more than just academic interest, particularly in AJAX applications where you can sometimes (often?) choose to process string data either server-side or client side.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.