This article describes how to use javascript to skillfully operate img for abnormal website submission. If A table needs to submit data to server A, it will be submitted to server B if server A cannot access it, it is a good choice to use js operation img to complete the process.
Use javascript to skillfully manipulate img for website exception submission
To submit data in A form to server A, if server A cannot access server B, submit the data to server B. To use JS to implement the aforementioned functions, you must consider cross-origin issues, I have used the xmlhttp method of ajax to detect that the remote file does not return 404. This method is not used due to cross-origin issues in ajax during testing, I have also used img's onerror and onload event solutions to handle this problem, but this solution will cause the submitted Form IE to pop up, And the browser intercepts the pop-up window, so I do not use this solution, what should I do? After a long time, the Javascript operation img is used to complete the process. The specific steps are as follows.
Step 1: Create a test.htm File
The content is as follows:
-
- <scriptsrcscriptsrc="getMessage.js"></script>
- <body>
- <formnameformname="mfrm">
- <inputidinputid="SendType"
- type="hidden"value="s129"name="SendType"/>
- <inputidinputid="title"name="title"/>
- <inputonClickinputonClick="returngetMessage(this.form);
- "type="button"name="imageField"/>
- </form>
- </body>
-
Step 2: Create the getMessage. js File
The content is as follows:
- FunctiongetMessage (frm ){
- Varsendtype = frm. SendType;
- Vartitle = frm. title;
- // Create a simulated form
- Varobjfrm = document. createElement ("form ");
- Varobjsendtype = document. createElement ("input ");
- Varobjtitle = document. createElement ("input ");
- // Use the img operation to process the server page of the target receiving data
- VarobjImg = document. createElement ("img ");
- ObjImg. id = "TmpSmsImg ";
- ObjImg. src = "http://www.xueit.com/images/logo.gif ";
- // Default server a image
- Document. body. appendChild (objImg );
- VarimgWidth = document. getElementById ("TmpSmsImg"). width;
- Document. body. removeChild (objImg );
-
- If (imgWidth = "210 ")
- // If the image exists, you can get the width equal to the predefined value,
- Page of server A receiving data by default
- {
- Objfrm. action = "http://www.xueit.com/testGet.aspx ";
- }
- Else // The image does not exist. The page of server B that receives data
- {
- Objfrm. action = "http://demo.xueit.com/testGet.aspx ";
- }
-