Welcome to WebmasterWorld Guest from 54.242.193.41

Forum Moderators: open

Message Too Old, No Replies

Making a Ajax request and manipulate the text

     
3:37 pm on Oct 18, 2018 (gmt 0)

New User

joined:Oct 13, 2018
posts:9
votes: 0


Hi,

I'm looking for a way to make a page request in Ajax and retrieve the request page to a string. This string should be available to any function but in the example below it isn't because alert(data); doesn't popup. After converting to a string i want to manipulate the text by using "find" to look for a string or a text in a determined area. Can this be done?


<input type="text" id="http" value="http://www.example.com">
<br>
<input type="button" id="submit" onclick="myhttp()" value="Send!"/>
<script>
function myhttp() {
var http = document.getElementById("http").value;
var dataString = 'lsd=AVrls4dL&email=email@gmail.com';

$.ajax({
type: "GET",
url: http,
data: dataString,
async: false,
success: function(data){
alert(data);
}
});

}
</script>


Thanks
3:44 pm on Oct 18, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Apr 1, 2016
posts:2624
votes: 774


Can this be done?

Yes.
My guess based on the code that you provided is that the alert doesn't pop-up because your request is not returning as a success. Have you checked that the request is successfully completed?
What type of data is the request returning? Is it html, JSON, a string?
9:32 pm on Oct 18, 2018 (gmt 0)

New User

joined:Oct 13, 2018
posts:9
votes: 0


Hi,

You are right the alert didn't popup because the request wasn't successfull.

> Have you checked that the request is successfully completed?
I added a error handle that it gives an error.

> What type of data is the request returning?
That's the question i can't get access to any type of data to work with.

> Is it html, JSON, a string?
The goal is to have access to the html page and automatically select the info/input fields data and show to the user (In one simple request).
10:10 pm on Oct 18, 2018 (gmt 0)

New User

joined:Oct 13, 2018
posts:9
votes: 0


Hi again,

Please tell me one thing, can i use JavaScript/Ajax to manipulate data, cookies and headers?

Thanks
1:23 am on Oct 19, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Apr 1, 2016
posts:2624
votes: 774


> What type of data is the request returning?
That's the question i can't get access to any type of data to work with.

This is a problem. If the url you are requesting is not returning anything then there is not much you can do. You need to fix that situation.

Is it html, JSON, a string?
The goal is to have access to the html page and automatically select the info/input fields data and show to the user (In one simple request).

Typically a AJAX request should return data that is then processed and displayed. It sounds like you would like to display a webpage from another website and simply allow your users to interact with it. This would be done with an Iframe not AJAX.
7:58 am on Oct 19, 2018 (gmt 0)

New User

joined:Oct 13, 2018
posts:9
votes: 0


Hi,

No iframe will not work, what i want to do is create one email field where user enters their email and a button where it starts the JavaScript. The JavaScript will then make a POST request to the site using the email and cache the page. From there i use a function "Find" or something similar looking for a specific text and copy that text to memory and only show the copied text to the user. Removing any obsolute code and add some CSS.

I have search online and found a Python script that does a similar task but i want to convert the code to JavaScript or PHP and from there develop the program that i want.
8:58 am on Oct 19, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Sept 25, 2005
posts:2091
votes: 370


Note that the data variable in the success function will not be available outside that function; you'd have to copy it to a separate variable.

As noted, the GET call is probably unsuccessful, and your script doesn't do anything when an an error occurs, so use error along with success.

Using your browser's JS console to read errors, and console.log() instead of alert(), will make your (programming) life a lot easier.
1:54 pm on Oct 19, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Apr 1, 2016
posts:2624
votes: 774


So basically it is as I said, you need to determine what data type the POST request returns before your able to go any further.

To find out, make a POST to the url and then check the response header using developer tools in Google Chrome or Firefox. The response header will show you all the pertinent information even in the case when the response fails.

Is the url you are POSTing to controlled by you or is it a 3rd party website?
6:31 pm on Oct 19, 2018 (gmt 0)

New User

joined:Oct 13, 2018
posts:9
votes: 0


Hi, The console give me 4 errors

1) Synchronous XMLHttpRequest on the main thread has been discontinued because of its negative effect on the user experience. For More Help [xhr.spec.whatwg.org...]

2) Blocked Crossover Request: Policy from the same source does not allow the remote resource to be read at http://www.example.com. (Reason: CORS request did not succeed).

3) NetworkError: A network error occurred.

4) undefined

[The 3, 4 are error handler console.log()]

The url that i'm posting is a 3rd party website.
8:31 pm on Oct 19, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Sept 25, 2005
posts:2091
votes: 370


The url that i'm posting is a 3rd party website.

If you're not allowed to, the browser will block the request. See Cross-Origin Resource Sharing (CORS) [developer.mozilla.org] for more information.
9:30 pm on Oct 19, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Apr 1, 2016
posts:2624
votes: 774


Well that partially answers the question:
Is the url you are POSTing to controlled by you or is it a 3rd party website?


One cannot indiscriminately receive responses from other domains. You need to permission, that is what CORS is for.
Read the link that Robzilla provided.

If the site you are posting to has granted permission to do so, then it must also have the CORS mechanisms in place to allow you to do so.
9:56 pm on Oct 20, 2018 (gmt 0)

New User

joined:Oct 13, 2018
posts:9
votes: 0


OK, while i was waiting for a response i decided to dig a bit more and found a similar script that is working just fine. It's written in Python, so i deduct that CORS is just a limitation for JavaScript. What about PHP? I tested file_get_contents but is blocked by the site. Also i need a post request.
10:18 pm on Oct 20, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Apr 1, 2016
posts:2624
votes: 774


No CORS is not language specific. You can get around CORS. I imagine that in Python that the script "fakes" being a real user, likely with Selenium, it submits the form and then takes the html in the response and then parses it with Beautifulsoup or another html parsing library and then returns to you the text or elements that you need. Just note that python script runs server side and all the request being sent will all be sent from your server's IP. So again without permission you will be blocked within a very short period of time (assuming you are making many requests).
9:10 am on Oct 21, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Sept 25, 2005
posts:2091
votes: 370


Why do you need a POST request if you're looking to retrieve data from this external website?
8:40 pm on Nov 11, 2018 (gmt 0)

New User

joined:Oct 13, 2018
posts:9
votes: 0


The site is [facebook.com...] and i want to retrieve the the fields

First Name, Last Name, Partial Email and Partial Number

After retrieving this fields i want to present this information to the user that requested.

Here is a similar project build in Python
[github.com...]
6:16 pm on Nov 13, 2018 (gmt 0)

New User

joined:Oct 13, 2018
posts:9
votes: 0


Didn't got any response, is anyone here in the forum?
6:23 pm on Nov 13, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Apr 1, 2016
posts:2624
votes: 774


Your post was a statement. No question therefore no response. Moreover, the Python script you linked to shows you exactly what you need to do.
Do you expect someone here to write the code for you and post it?