简体   繁体   English

我可以将数据发送到服务器iphone上的javascript函数吗

[英]can i send data to a javascript function on the server iphone

I i'm wondering if it is possible to call a javascript function from my application. 我想知道是否可以从我的应用程序中调用javascript函数。 The js function is on the server. js函数在服务器上。 Let's say i have a some inputs in the app. 假设我在应用程序中有一些输入。 Then i have this submit button which calls the IBAction, from the IBAction i want to call: 然后,我有一个提交按钮,它从我要调用的IBAction中调用了IBAction:

function mySubmit(){
    document.getElementById("myForm").submit();
}

and pass it the data the user entered and then get back a response (in my app) not in the server 并将用户输入的数据传递给它,然后返回不在服务器中的响应(在我的应用中)

Is this possible to do? 这可能吗? if so, can you provide some useful links? 如果可以,您能否提供一些有用的链接?

Thanks in advance and have a nice saturday ;) 在此先感谢,祝您星期六愉快;)

The js function is on the server js函数在服务器上

Are you sure this is your case? 您确定这是您的情况吗? Don't you mean that your js submits a form that is itself treated on the server side? 您不是说您的js提交了一种本身在服务器端处理的表单吗? Javascript is normally a client-side scripting language, which is executed on client-side, contrary to eg PHP which is only executed server-side. Javascript通常是一种客户端脚本语言,它在客户端执行,而PHP仅在服务器端执行。

[EDIT] (As stated by @Dr.Dredel in the comments, as there exists server-side javascript, but these usages are not yet very common and I don't think it corresponds to your context) [编辑](正如@ Dr.Dredel在评论中所述,因为存在服务器端javascript,但是这些用法还不是很常见,我认为它与您的环境不符)

If you need to call a javascript function from an already loaded HTML page in a WebView, you can simply use the UIWebView's stringByEvaluatingJavascriptFromString: . 如果您需要从WebView中已加载的HTML页面中调用javascript函数,则只需使用UIWebView的stringByEvaluatingJavascriptFromString: You can pass a string representing javascript code to this function, so you can build a string that represent a function call with string or int arguments for example without any problem. 您可以将表示javascript代码的字符串传递给此函数,因此可以构建一个表示具有string或int参数的函数调用的字符串,例如,没有任何问题。

But if you intend to load your HTML page just to call a javascript fonction which in turn submit a form that sends the form's data to your server… This is clearly the wrong way! 但是,如果您打算加载HTML页面只是为了调用javascript函数,而该函数又提交了一个表单,该表单会将表单的数据发送到您的服务器……这显然是错误的方式!

You should instead considering performing the NSURLRequest to your server directly in Objective-C (maybe using ASIHTTPRequest to perform a POST request and easily set the values for each keys of the form you intend to send). 相反,您应该考虑直接在Objective-C中对服务器执行NSURLRequest (也许使用ASIHTTPRequest执行POST请求并轻松设置要发送的表单的每个键的值)。 In addition, doing this directly in ObjC will avoid loading the HTML page for nothing and rely on the js script, and will allow you to directly get the response in the delegate method. 另外,直接在ObjC中执行此操作将避免加载HTML页面,而无需依赖js脚本,并且将允许您直接在委托方法中获取响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM