Solved

JQuery AJAX load and split at the same time

Posted on 2014-09-20
6
176 Views
Last Modified: 2014-09-21
Using Jquery I call a server side function to return some text by loading it into an ID as follows:

$('#someid').load('/lookup.asp?ID=' + ID);

Open in new window


All works fine. However the output of the server side function will be surrounded by special string delimiters and I would like to split the string so only the text within the delimiters is loaded.

So for example the output could be:

<p>Some start text</p>
### START ###
<p>The payload text.</p>
### END ###
<p>Some end text.</p>

Open in new window


But what I would like is to only load the following:

<p>The payload text.</p>

Open in new window

0
Comment
Question by:mike99c
6 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40334402
Something simple like

theresult=thestring.split("### START ###\n")[1].split("\n### END ###")[0];

Open in new window


Only problem is the string will be injected in to the element straight away, so you either have to use .ajax and format the response before inserting it or format the string as it appears in the someid element in a callback when received
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 40334403
So parse it server side and only return what you want to display.  You could also parse id it clent side but the overhead would then negate part of the benefit you get by getting the data in background with AJAX.

Cd&
0
 

Author Comment

by:mike99c
ID: 40334410
I can't parse it server side as the rest of the output I want to strip out is in fact the HTML of the page. Which is why the server script outputs the text with the delimiters.

I am aware about string split functions but can I somehow attach it to the load function? If not how do I copy the server side output to a local variable?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40334415
$.ajax({
	url: '/lookup.asp?ID=' + ID,
	success: function(response) {
		theresult=response.split("### START ###\n")[1].split("\n### END ###")[0];
		$('#someid').html(theresult);
	}
});

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 40335009
@Gary you pollute the global scope when you do not add var in front of local vars
0
 

Author Closing Comment

by:mike99c
ID: 40335215
Made a slight change:

$.ajax({
      url: '/lookup.asp?ID=' + ID,
      success: function(response) {
            var theresult=response.split("### START ###\n")[1].split("\n### END ###")[0];
            $('#someid').html(theresult);
      }
});
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question