PDA

View Full Version : xmlhttp problem with different browser



daytonatkf
09-25-2012, 09:41 AM
i try to capture the return value from a server, this is how it work, is a sms API, when i want to check some sms status, need to access some url with some parameter (.aspx), the page will return some value with the same url , e.g of return value will be like 1122:12345647, when i view source , it show only the value...

i trying to grape that value so i can store it in the database, i tried this method xmlhttp, it work in the IE, i can show the return value and store it, but it doesnt work on FF and Chrome. I try in the live server as well , it still doesn't work. Do i need any adjustment or any other method to recommend?

Code:

function httpGet(theUrl)
{
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false);
xmlHttp.send(null);
return xmlHttp.responseText;
}

thanks

bernie1227
09-25-2012, 09:54 AM
Please use the forum's bbcode tags to make it more readable:

for php code............
<?php /* code goes here */ ?>
for html...............
<!-- markup goes here -->.....
for js/css/other.......
code goes here................


[Moderator's Note - No longer relevant (Formatted)]

jscheuer1
09-25-2012, 02:46 PM
That looks to me as what's called a synchronous request. It relies upon the server. If the page isn't on a server, it might not work in some browsers.

You should be aware that a request of that type halts processing on the page until it's fulfilled. Though a little more complicated, an asynchronous request is usually a better idea.

I would suggest using jQuery to carry out a request, but here's a good method for it using only javascript:

http://www.dynamicdrive.com/forums/showthread.php?19247-Ajax&p=190183#post190183

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.

daytonatkf
09-26-2012, 08:01 AM
thanks for the reply, i try the guidelines and implement it into code...it work in IE, but FF and Chrome show return empty value.....the page that i try to access actually located in different domain than my domain. the sms API are third party with .aspx page. Do u think xmlhttp will work for different browser? e.g i go to this url "www.smsapi/websmsapi/creditsLeft.aspx?username=username&password=password",the same url page will return and show the credit amount. the page is just empty and show only credit left amount with the same url.

thanks for help..

daytonatkf
09-28-2012, 08:35 AM
[broken link removed] try some method here, but still dont return any value in FF and Chrome, some return as it doesnt support ajax , message. Any suggestion or basic method that can make it run on IE, FF and Chrome? thanks

bernie1227
09-28-2012, 08:39 AM
it's a dead link

daytonatkf
09-28-2012, 09:01 AM
http://www.dynamicdrive.com/forums/showthread.php?19247-Ajax&p=190183#post190183

sorry..thats the post i referring to....i try at local and live server...nothing work...
xmlhttp i mention, it work for IE browser...but chrome n ff show return nothing... please guide me....
the url i need to access is .aspx file, it will return some value, this file is store are different domain.

Any way can show that IE , FF and chrome can capture return value from different domain?

thanks

daytonatkf
09-28-2012, 09:37 AM
<p><b>SMS.</b><b><br />
</b>
</p>
<hr/>
<p>&nbsp;</p>

{literal}
<script language="javascript">
function httpGet(theUrl)
{
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false);
xmlHttp.send(null);
return xmlHttp.responseText;
}

</script>
{/literal}

{php}
global $Q;
if ($Q->get_permission('crm_sms','readcontent') == '1')
{
{/php}

{foreach from=$sms_account item=type }
Account : {$type.name} <br />
{literal}
<script language="javascript">

document.write('Credit(s) left : '+httpGet("http://globalsms.com.my/websmsapi/creditsLeft.aspx?username={/literal}{$type.user}{literal}&password={/literal}{$type.pass}{literal}")+'.0');
</script>
{/literal}
<br/><br />

{/foreach}
{php}

}
else
{
echo 'Permission not allowed.';
}
{/php}

those are the code i use, is a tpl page....and i try it it local and live server also.....the sms page are from different domain.

thanks

traq
09-28-2012, 02:36 PM
...the page that i try to access actually located in different domain than my domain.

you should be aware that this usually prevents ajax calls, due to security issues. Almost all of javascript relies on a "same-origin policy" (dealing with only the current domain).

Two options I see:

1) See if your third party API will support cross-origin resource sharing. Mozilla has a good explanation of CORS (https://developer.mozilla.org/en-US/docs/HTTP_access_control).

2) Configure your server to send an HTTP request to access the API. With PHP, this can be done via the cURL extension. I know it is possible to achieve in most server-side web languages, though I don't know how it works in other languages (are you using a server-side language now? you mentioned the API was an asp site, but now that I re-read your posts, it seems your site might be static [client-side scripting only]...?).

Once you make the API call from your server, you page can access the results via ajax normally.

jscheuer1
09-28-2012, 02:37 PM
AJAX generally won't work on a page from another domain. And it's technically illegal to grab another domain's content as your own. It's copyright infringement. That is unless the content you're grabbing is also yours, just happens to be on a different domain or you've subscribed to it legally for use on your site as opposed to for personal use.

But AJAX alone still cannot do this, you would need some server side code to first grab the external domain's page's content to a server side file and/or server side script. From that script and/or file - now on your domain, you can import the content via AJAX to a page.

If that's what you're already doing, it may just be a matter of timing. Is the external content transferred to your domain before the AJAX request for it is executed . . . If not you need mechanisms in place to make sure that it is or a way of polling it to see when it arrives.

I'll have a look at your code and see if it reveals anything else to me.

OK I did -

It looks like you're using php. If so you could try a file_get_contents. It too can be limited to same domain, but not always. That depends upon your server's settings. It's ultimate success, assuming your server allows it, depends upon the external domain not blocking it.

The bottom line though, if this is an API service, why not just use the API, why complicate matters with AJAX?

traq
09-28-2012, 04:01 PM
It looks like you're using php...

that's what I thought, at first. But what he posted is a templating script (I think it's Smarty). Yes, the template is parsed and then executed via PHP, but that doesn't mean he's actually using PHP directly anywhere.

You're right about file_get_contents (http://php.net/file_get_contents)(), though. If it's a simple GET request, then that would be easier than using the cURL library (I'm just used to doing it that way).

jscheuer1
09-28-2012, 04:25 PM
Well PHP should be available then, the syntax for it in the template just different, or perhaps not. But those tokens in the template probably resolve to actual PHP tags in the pages the template is used to create.

I buried the lead though. If this is an API, just use the API. No AJAX or AJAXy stuff need be used. The template itself should be able to provide all the substitutions necessary for each page to have a unique usage if required.

If all else fails, put the darn thing in an iframe and be done with it.

traq
09-28-2012, 06:53 PM
Well PHP should be available then, the syntax for it in the template just different, or perhaps not. But those tokens in the template probably resolve to actual PHP tags in the pages the template is used to create.Smarty parses the template before it's run through the PHP engine itself; I'm pretty sure (http://www.smarty.net/syntax_comparison) you can't use *just any* PHP in it. But I've never used it. Since PHP is basically a templating system in the first place, I don't see the point in putting another templating engine on top of it.
- Yes, it's (arguably) simpler, but I think it's much more beneficial to just learn PHP.
- I've heard the argument that it's useful when you have separate coding and layout teams working on a project, but if you've got such a large team, why not build a proper View layer?

anyway... :)


I buried the lead though. If this is an API, just use the API. No AJAX or AJAXy stuff need be used. The template itself should be able to provide all the substitutions necessary for each page to have a unique usage if required.

If all else fails, put the darn thing in an iframe and be done with it.

agreed.

daytonatkf
10-01-2012, 01:57 AM
i do use the API to get the value, or the url i provided doesn't look like an API???? when i use web to go to the url, it will show / return a value. I need to grab that value and store it in database. Since many mention about about security...but IE manage to grab that value....Is there any code or method can use to grab that value in FF and Chrome?
thanks

traq
10-01-2012, 02:09 AM
well, answer me this: are you using php (beyond the templating code you showed in your previous posts)? If so, then -as John says- file_get_contents (http://php.net/file_get_contents)() is probably your best bet.

If not, then you should check with the sms service to find out if they offer a javascript interface to their API.

In the end, if you can't figure it out, then the sms service is who you need to be asking. [hopefully] They will know how to make the best use their API. We can offer generic suggestions, but without knowing the API, we couldn't say what would actually work and what wouldn't.

daytonatkf
10-01-2012, 03:12 AM
well, answer me this: are you using php (beyond the templating code you showed in your previous posts)? If so, then -as John says- file_get_contents (http://php.net/file_get_contents)() is probably your best bet.

If not, then you should check with the sms service to find out if they offer a javascript interface to their API.

In the end, if you can't figure it out, then the sms service is who you need to be asking. [hopefully] They will know how to make the best use their API. We can offer generic suggestions, but without knowing the API, we couldn't say what would actually work and what wouldn't.

Is php, but the file .tpl , template , smarty. I can use other than php language. i try to use file_get_contents, but it show some message like failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

jscheuer1
10-01-2012, 03:49 AM
Do you have or could you make up an ordinary page with this thing on it, whatever it is, that doesn't use an XMLHttpRequest?

What is this thing anyway? Do you have permission to use it in this manner? Is it part of some sort of widget? If so, what's the widget do?

daytonatkf
10-01-2012, 04:30 AM
Do you have or could you make up an ordinary page with this thing on it, whatever it is, that doesn't use an XMLHttpRequest?

What is this thing anyway? Do you have permission to use it in this manner? Is it part of some sort of widget? If so, what's the widget do?

This is a SMS module. The sms api is at other domain. i try with the first test, so i check the balance credit, with the url i provide, it will return the balance value after you go to the url i provided (but require username n pass), i want to capture that value in my website and store it in database. If doesnt use xmlhttp method, what else i can use....among so many method, only this method successful return the value...but for IE only...It i can get the value from that url...then i will proceed to next task. But now i stuck at the point to get the return value..

any idea? is it browser problem?

jscheuer1
10-01-2012, 07:31 AM
What sort of contract do you have with this other site?

daytonatkf
10-01-2012, 08:06 AM
What sort of contract do you have with this other site?

they are online sms service provider, 3rd party...i use their service to send sms...i want to intergrate the sms with my web site, so i can monitor the sms process, is this related?

jscheuer1
10-01-2012, 04:25 PM
Any 3rd party service paid or free has terms of service including disclaimers. These are the contract. In the forums here we're not allowed to help folks violate these contracts. Since you're having difficulty doing what you want to do, I can only assume you either haven't read the documentation and/or are trying something that's not supported. In addition, you may or may not be trying something that's not allowed by the TOS.

So yes, it's all related. How do you sign up for this service? Where are it's documentation and rules posted?

daytonatkf
10-02-2012, 01:31 AM
Any 3rd party service paid or free has terms of service including disclaimers. These are the contract. In the forums here we're not allowed to help folks violate these contracts. Since you're having difficulty doing what you want to do, I can only assume you either haven't read the documentation and/or are trying something that's not supported. In addition, you may or may not be trying something that's not allowed by the TOS.

So yes, it's all related. How do you sign up for this service? Where are it's documentation and rules posted?

Nothing so serious....just sign up for local package, mean i can only send sms to people in my country only. The documentation is nothing, just show API and their parameters stuffs...It didn't mention about what CAN or CANT do for their client. Their documentation didnt mention much about technical info. Mostly like faq , charges , contact and some basic info. Can u recommend what i can do now, so let me try it.

traq
10-02-2012, 02:03 AM
Can u recommend what i can do now, so let me try it.
in addition to documentation, did you read their Terms of Service agreement?

Does the documentation's API examples include any javascript implementations?

your next step would be to contact them.

daytonatkf
10-02-2012, 02:09 AM
in addition to documentation, did you read their Terms of Service agreement?

Does the documentation's API examples include any javascript implementations?

your next step would be to contact them.

it didnt mention about javascript, it mention those API parameters, by the way there is no terms or service agreement... i didn't know it going to be so serious regarding those legal stuffs.

Anyways, thats for the guidelines , i will contact them.